在 Java 中通过键盘填充数组

Fill an array from keyboard in Java

早上好, 我是编程新手。 我正在尝试编写一个程序,从键盘读取一些数字并将它们相加。我开始编写一个程序来对数字求和,我的问题是我不知道如何配置扫描仪来填充 array/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);
   }

}

I don't know how to configure the Scanner to fill an array/ArrayList. How could i do?

在用户收到提示时填充 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();

但是,请注意数组的大小是固定的。所以你不想提示超过给定数组大小的提示。

如果提示用户的次数未知,请使用带有数组列表的 while 循环。