Arrays 用Java从键盘填充数组
早上好, 我是编程新手。 我正试图写一个程序,从键盘上读取一些数字,并将它们相加。我开始写一个程序来求和,我的问题是我不知道如何配置扫描仪来填充数组/数组列表。 我怎么办? 这是我的密码Arrays 用Java从键盘填充数组,arrays,arraylist,numbers,fill,Arrays,Arraylist,Numbers,Fill,早上好, 我是编程新手。 我正试图写一个程序,从键盘上读取一些数字,并将它们相加。我开始写一个程序来求和,我的问题是我不知道如何配置扫描仪来填充数组/数组列表。 我怎么办? 这是我的密码 class array { public static void main(String[] args) { int start = 0; int [] interi = {2,3,6,10,24,45}; for (int i:interi) start+=i;
class array {
public static void main(String[] args) {
int start = 0;
int [] interi = {2,3,6,10,24,45};
for (int i:interi)
start+=i; //++
System.out.println(start);
}
}
我不知道如何配置扫描仪来填充数组/数组列表。我怎么办
要在提示用户时填充ArrayList,请执行以下操作:
//example, prompt user 3 times
ArrayList<Integer> list = new ArrayList<>();
for(int i=0; i<3; i++)
list.add(scn.nextInt());
但是,请注意数组的大小是固定的。因此,您不希望提示超过给定的数组大小
如果要提示用户未知次数,请使用带有数组列表的while循环
array[i] = scn.nextInt();