next()与nextLine()

package scanner;

import java.util.Scanner;

public class Demo1 {
    public static void main(String[] args) {
       //创建一个扫描器对象,用来接收键盘数据
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用next方法来接:");
        //判断用户有没有输入字符
        if(scanner.hasNext()){
            //使用next接收
            String s = scanner.next();
            System.out.println("输入的内容为:"+s);
        //凡是属于IO流的用完要关掉
            scanner.close();
        }
    }
}

next()与nextLine()

nextLine()

package scanner;

import java.util.Scanner;

public class Demo2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用nextLine方法来接:");
        if(scanner.hasNextLine()){
            String s = scanner.nextLine();
            System.out.println("输入的内容为:"+s);
            scanner.close();
        }
    }
}

next()与nextLine()

 

上一篇:1076 Forwards on Weibo


下一篇:1076 WIFI密码