先说结论:
- 两者均根据空格划分数据
- 两者在没有数据输入时均会等待输入
- next()方法会将空格划分的数据依次输出,运行一次,输出一个
- 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()); }
}