一行输入,回车结束
输入:输入的数据,用空格隔开
输出:存入数组中
Scanner sc = new Scanner(System.in);
String inputString = sc.nextLine();
String stringArray[] = inputString.split(" ");
int num[] = new int[stringArray.length];
for (int i = 0; i < stringArray.length; i++) {
num[i] = Integer.parseInt(stringArray[i]);
}
for (int i =0;i<num.length;i++){
System.out.println(num[i]);
}
逐行输入,-1结束
System.out.println("Enter a space separated list of numbers:");
Scanner in =new Scanner(System.in);
int a=0;
while (true){
a=in.nextInt();//输入一个整数
if (a==-1){
break;
}
if(a==0){
System.out.println("空");
continue;
}
System.out.println(a);
}
System.out.println("The numbers were:");