java Scanner.next() 和 Scanner.nextLine 的区别

目录

.next() 和 .nextLine

next()

 nextLine()

.hasNext() 和 .hasNextLine

.hasNext()的使用

.hasNextLine()的使用


.next() 和 .nextLine

next()的使用

next() 读取空格前的内容

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.next();
        System.out.println(s);
    }

java Scanner.next() 和 Scanner.nextLine 的区别

 nextLine()的使用

nextLine()  读取一整行

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        System.out.println(s);
    }

.next() 和 .nextLine 的区别  nextLine()  读取一整行 next() 读取空格前的内容

.hasNext() 和 .hasNextLine

.hasNext()的使用

hasNext() 配合 next() 使用 和while 循环使用

一直判断当前行的数据,直到当前行没有数据

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()) {
            System.out.println(scanner.next());
        }
    }

java Scanner.next() 和 Scanner.nextLine 的区别

.hasNextLine()的使用

hasNext() 配合 next() 使用 和while 循环使用 一直判断输入的数据,一次读取一行,直到停止输入数据

停止输入快捷键 Ctrl + D

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNextLine()) {
            System.out.println(scanner.nextLine());
        }
    }

java Scanner.next() 和 Scanner.nextLine 的区别

本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://www.net2asp.com/fb1e6c357f.html