建立一个新项目并编写一个累加类,用JAVA从键盘输入10个数,用数组保存,并求出它们的和

2024-11-17 09:58:01
推荐回答(1个)
回答(1):

try {
int sum = 0;
int[] aa = new int[10];
for (int i = 1; i <= 10; i++) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
aa[i - 1] = n;
sum = sum + n;
}

System.out.println("sum=" + sum);
} catch (Exception e) {
e.printStackTrace();
}