读取整数型
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);//in java.io.inputStream
int a=0, b=0;
while(sc.hasNextInt())
{
a = sc.nextInt();
b = sc.nextInt();
System.out.println(a);
System.out.println(b);
}
}
input:
1 2
output:
1
2
读取字符型
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);//in java.io.inputStream
String str ;
while(sc.hasNext())
{
str = sc.nextLine();
System.out.println(str);
}
}
input :
ni hao
output :
ni hao
要是直接输入一次就结束的就可去掉循环体。需要重复输入的就像代码中一样即可。