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);