用Java编程声明一个int型的二维数组循环接收8个学生的成绩计算并输出这8个学生的总分以及平均分最高分和最低分。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[][] scores = new int[8][3]; // 声明一个二维数组,8个学生,每个学生有3门成绩
Scanner input = new Scanner(System.in);
for (int i = 0; i < 8; i++) {
System.out.printf("请输入第%d个学生的成绩:\n", i + 1);
for (int j = 0; j < 3; j++) {
System.out.printf("请输入第%d门成绩:", j + 1);
scores[i][j] = input.nextInt(); // 循环接收8个学生的成绩
}
}
int totalScore = 0; // 所有学生的总分
int maxScore = scores[0][0]; // 最高分
int minScore = scores[0][0]; // 最低分
for (int i = 0; i < 8; i++) {
int sum = 0; // 每个学生的总分
for (int j = 0; j < 3; j++) {
sum += scores[i][j];
if (scores[i][j] > maxScore) {
maxScore = scores[i][j];
}
if (scores[i][j] < minScore) {
minScore = scores[i][j];
}
}
totalScore += sum;
}
double averageScore = (double) totalScore / 24.0; // 所有学生的平均分
System.out.printf("所有学生的总分为:%d\n", totalScore);
System.out.printf("所有学生的平均分为:%.2f\n", averageScore);
System.out.printf("最高分为:%d\n", maxScore);
System.out.printf("最低分为:%d\n", minScore);
}
}
``
原文地址: https://www.cveoy.top/t/topic/eRVA 著作权归作者所有。请勿转载和采集!