早上好,我是编程新手。我正在编写一个程序,它从键盘上读取一些数字,并将它们加在一起。我写了一个程序来计算数字,我的问题是我不知道如何配置Scanner来填充数组/ArrayList。我该怎么做?这是我的密码。
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);
}
}发布于 2017-10-18 01:04:17
我不知道如何配置Scanner来填充数组/ArrayList。我该怎么做?
若要在用户收到提示时填充ArrayList:
//example, prompt user 3 times
ArrayList<Integer> list = new ArrayList<>();
for(int i=0; i<3; i++)
list.add(scn.nextInt());如果使用数组:
array[i] = scn.nextInt();但是,注意数组的大小是固定的。因此,您不希望提示大于给定的数组大小。
如果要提示用户的次数未知,请使用带数组列表的with循环。
https://stackoverflow.com/questions/46799130
复制相似问题