1.已知有3个班级各5名学员,请使用
二维数组计算各个班级的总成绩
提示:二维数组
- 第一种:
public static void main(String[] args) {
int[][] arr = new int[3][5];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
System.out.println("******第" + (i + 1) + "个班");
// int sum = 0;
for (int j = 0; j < arr[i].length; j++) {
System.out.print("请输入第" + (j + 1) + "个学生的成绩:");
int score = sc.nextInt();
arr[i][j] = score;
// sum = sum + arr[i][j];
}
// System.out.println("******第"+(i+1)+"个班的成绩为:"+sum);
}
System.out.println("成绩统计:");
for (int i = 0; i < arr.length; i++) {
// 默认初始班级总成绩为0
int scoreSum = 0;
for (int j = 0; j < arr[i].length; j++) {
scoreSum = scoreSum + arr[i][j];
}
System.out.println("第" + (i + 1) + "个班的总成绩为:" + scoreSum);
}
- 第二种:
public static void main(String[] args) {
int[][] arr = new int[3][5];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
System.out.println("******第" + (i + 1) + "个班");
int sum = 0;
for (int j = 0; j < arr[i].length; j++) {
System.out.print("请输入第" + (j + 1) + "个学生的成绩:");
int score = sc.nextInt();
arr[i][j] = score;
sum = sum + arr[i][j];
}
System.out.println("******第"+(i+1)+"个班的成绩为:"+sum);
}