• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

java.util.Scanner中hasNext()方法和next()方法的区别

开发技术 开发技术 1个月前 (08-11) 28次浏览
先说结论:
  1. 两者均根据空格划分数据
  2. 两者在没有数据输入时均会等待输入
  3. next()方法会将空格划分的数据依次输出,运行一次,输出一个
  4. hasNext()方法会跟着next()方法移动,当前数据不为空,则返回true
代码:
import java.util.Scanner;

public class MyScanner {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("1. System.out.println(scanner.hasNext());");
        System.out.println(scanner.hasNext());
        System.out.println("2. System.out.println(scanner.hasNext());");
        System.out.println(scanner.hasNext());
        System.out.println("3. System.out.println(scanner.next());");
        System.out.println(scanner.next());
        System.out.println("4. System.out.println(scanner.hasNext());");
        System.out.println(scanner.hasNext());
        System.out.println("5. System.out.println(scanner.next());");
        System.out.println(scanner.next());
        System.out.println("6. System.out.println(scanner.hasNext());");
        System.out.println(scanner.hasNext());
        System.out.println("7. System.out.println(scanner.next());");
        System.out.println(scanner.next());
        System.out.println("8. System.out.println(scanner.hasNext());");
        System.out.println(scanner.hasNext());

    }
}

  

 
结果:

 

java.util.Scanner中hasNext()方法和next()方法的区别

 

流程

 

java.util.Scanner中hasNext()方法和next()方法的区别


喜欢 (0)