我想一下子输入都个数;不想这么写,有没有简单点的方法
Scanner in = new Scanner(System.in);
int a = in.nextInt();
Scanner in1 = new Scanner(System.in);
int b = in.nextInt();
Scanner in2 = new Scanner(System.in);
int c = in.nextInt();
Scanner in = new Scanner(System.in); 这样只要写一个就可以了,之后的只要有数字就可以一直nextInt
int a = in.nextInt();
Scanner in1 = new Scanner(System.in);
int b = in.nextInt();
Scanner in2 = new Scanner(System.in);
int c = in.nextInt();
Scanner in = new Scanner(System.in);
int j=0;
int[] a = new int[3];//输入几个,数组多大
while(in.hasNext()){
a[j]=in.nextInt();
j++;
}输入过程数字之间使用空格 最后结束是按Ctrl+Z
用循环语句可以一次多输几个
呃。。其实你可以这样写的:
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
int[] nums = new int[3];
for (int i = 0; i < 3; i++)
{
Scanner in = new Scanner(System.in);
nums[i] = in.nextInt();
}
Scanner sc=new Scanner();for(int i=0;i<5;i+r){int a=sc.nextInt();System.out.println(a);}
除了上面的方法,可以考虑用分割符号,比如逗号分隔几个数输入,获取的时候获取这一行字符串,然后分隔成你想要的数字。
Scanner sc=new Scanner();
for(int i=0;i<5;i+r)
{int a=sc.nextInt();System.out.println(a);
}