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

先说结论:
  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()方法的区别

上一篇:Java Thread中,run方法和start方法的区别


下一篇:数据库表设计时一对一关系存在的必要性 数据库一对一、一对多、多对多设计 面试逻辑题3.31 sql server 查询某个表被哪些存储过程调用 DataTable根据字段去重 .Net Core Cors中间件解析 分析MySQL中哪些情况下数据库索引会失效