Class Scanner
java.lang.Object
java.util.Scanner
All Implemented Interfaces:
Closeable, AutoCloseable, Iterator
其他不說,實現了Iterator接口
boolean hasNext()
Returns true if the iteration has more elements.
E next()
Returns the next element in the iteration.
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();//回車結束輸入,計入空格,是一行輸入
int age = scan.nextInt();//空格結束,鍵盤鍵入有效數字開始,
//如果鍵入 abc(非數字字符會報錯)所以有 hasNextInt();
String name = scan.next(); //輸入一行如果以空格間隔,只會取到空格之前的字符,后面并不會取到。
實列:輸入數字以0結束
1
2
3
4
0
import java.util.Scanner;
Scanner scan = new Scanner(System.in);
String str = "";
int i = 1;
do{
i = scan.nextInt();
str += i;
}while( i != 0);
System.out.println(str);
輸入兩個數 n和b
import java.util.Scanner;
Scanner scan = new Scanner(System.in);
int n = 0 ;
int b = 0;
n = scan.nextInt();
b = scan.nextInt();
scan.close();
System.out.println(n +b);