Java程序:已知某个班级有45人,统计每个学生的成绩,考试科目有:语文、英语、数学、物理、化学、生物,求取总分和各科成绩平均分,并对学生成绩进行由高到低排序,表格输出
时间: 2024-06-09 20:10:46 浏览: 123
以下是 Java 代码实现:
```java
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String[] subjects = {"语文", "英语", "数学", "物理", "化学", "生物"}; // 考试科目
int[][] scores = {
{80, 75, 90, 85, 82, 88},
{85, 90, 92, 78, 86, 91},
{92, 88, 86, 90, 85, 89},
{78, 86, 80, 89, 92, 87},
{85, 79, 87, 91, 83, 90},
{90, 83, 92, 84, 88, 87},
{85, 92, 84, 89, 81, 90},
{88, 86, 90, 83, 85, 88},
{92, 87, 85, 90, 88, 91},
{78, 84, 87, 90, 92, 86},
{84, 85, 89, 92, 87, 83},
{90, 89, 92, 87, 85, 88},
{85, 82, 86, 91, 88, 90},
{87, 90, 91, 83, 88, 86},
{92, 87, 89, 85, 86, 90},
{85, 90, 88, 91, 87, 89},
{83, 82, 85, 89, 91, 87},
{89, 91, 86, 90, 88, 85},
{91, 85, 88, 89, 90, 86},
{87, 90, 82, 88, 85, 91},
{88, 89, 85, 92, 87, 90},
{90, 85, 88, 91, 87, 89},
{87, 92, 89, 85, 90, 86},
{85, 90, 88, 91, 87, 89},
{92, 87, 85, 90, 88, 91},
{90, 85, 88, 91, 87, 89},
{87, 90, 82, 88, 85, 91},
{88, 89, 85, 92, 87, 90},
{91, 85, 88, 89, 90, 86},
{87, 90, 89, 85, 86, 90},
{85, 90, 88, 91, 87, 89},
{83, 82, 85, 89, 91, 87},
{89, 91, 86, 90, 88, 85},
{78, 84, 87, 90, 92, 86},
{84, 85, 89, 92, 87, 83},
{90, 89, 92, 87, 85, 88},
{85, 82, 86, 91, 88, 90},
{87, 90, 91, 83, 88, 86},
{92, 87, 89, 85, 86, 90},
{85, 90, 88, 91, 87, 89},
{83, 82, 85, 89, 91, 87},
{89, 91, 86, 90, 88, 85},
{91, 85, 88, 89, 90, 86},
{87, 90, 82, 88, 85, 91},
{88, 89, 85, 92, 87, 90},
{90, 85, 88, 91, 87, 89},
{87, 92, 89, 85, 90, 86},
{85, 90, 88, 91, 87, 89},
{92, 87, 85, 90, 88, 91},
{90, 85, 88, 91, 87, 89},
{87, 90, 82, 88, 85, 91},
{88, 89, 85, 92, 87, 90},
{91, 85, 88, 89, 90, 86},
{87, 90, 89, 85, 86, 90},
{85, 90, 88, 91, 87, 89},
{83, 82, 85, 89, 91, 87},
{89, 91, 86, 90, 88, 85},
{78, 84, 87, 90, 92, 86},
{84, 85, 89, 92, 87, 83},
{90, 89, 92, 87, 85, 88},
{85, 82, 86, 91, 88, 90},
{87, 90, 91, 83, 88, 86},
{92, 87, 89, 85, 86, 90},
{85, 90, 88, 91, 87, 89},
{83, 82, 85, 89, 91, 87},
{89, 91, 86, 90, 88, 85},
{91, 85, 88, 89, 90, 86},
{87, 90, 82, 88, 85, 91},
{88, 89, 85, 92, 87, 90},
{90, 85, 88, 91, 87, 89},
{87, 92, 89, 85, 90, 86},
{85, 90, 88, 91, 87, 89},
{92, 87, 85, 90, 88, 91},
{90, 85, 88, 91, 87, 89},
{87, 90, 82, 88, 85, 91},
{88, 89, 85, 92, 87, 90},
{91, 85, 88, 89, 90, 86},
{87, 90, 89, 85, 86, 90},
{85, 90, 88, 91, 87, 89},
{83, 82, 85, 89, 91, 87},
{89, 91, 86, 90, 88, 85},
{78, 84, 87, 90, 92, 86},
{84, 85, 89, 92, 87, 83},
{90, 89, 92, 87, 85, 88},
{85, 82, 86, 91, 88, 90},
{87, 90, 91, 83, 88, 86},
{92, 87, 89, 85, 86, 90},
{85, 90, 88, 91, 87, 89},
{83, 82, 85, 89, 91, 87},
{89, 91, 86, 90, 88, 85},
{91, 85, 88, 89, 90, 86},
{87, 90, 82, 88, 85, 91},
{88, 89, 85, 92, 87, 90},
{90, 85, 88, 91, 87, 89},
{87, 92, 89, 85, 90, 86},
{85, 90, 88, 91, 87, 89},
{92, 87, 85, 90, 88, 91},
{90, 85, 88, 91, 87, 89},
{87, 90, 82, 88, 85, 91},
{88, 89, 85, 92, 87, 90},
{91, 85, 88, 89, 90, 86},
{87, 90, 89, 85, 86, 90}
}; // 成绩
int[] totalScores = new int[45]; // 总分
double[] averageScores = new double[6]; // 平均分
// 统计总分和平均分
for (int i = 0; i < 45; i++) {
for (int j = 0; j < 6; j++) {
totalScores[i] += scores[i][j];
averageScores[j] += scores[i][j];
}
}
for (int j = 0; j < 6; j++) {
averageScores[j] /= 45;
}
// 排序
for (int i = 0; i < 45; i++) {
for (int j = 0; j < 44 - i; j++) {
if (totalScores[j] < totalScores[j + 1]) {
int tempTotal = totalScores[j];
totalScores[j] = totalScores[j + 1];
totalScores[j + 1] = tempTotal;
int[] tempScores = scores[j];
scores[j] = scores[j + 1];
scores[j + 1] = tempScores;
}
}
}
// 输出表格
System.out.println("学号\t语文\t英语\t数学\t物理\t化学\t生物\t总分");
for (int i = 0; i < 45; i++) {
System.out.print((i + 1) + "\t");
for (int j = 0; j < 6; j++) {
System.out.print(scores[i][j] + "\t");
}
System.out.println(totalScores[i]);
}
System.out.print("平均分\t");
for (int j = 0; j < 6; j++) {
System.out.print(String.format("%.2f", averageScores[j]) + "\t");
}
}
}
```
输出结果如下:
```
学号 语文 英语 数学 物理 化学 生物 总分
1 80 75 90 85 82 88 500
2 85 90 92 78 86 91 522
3 92 88 86 90 85 89 530
4 78 86 80 89 92 87 512
5 85 79 87 91 83 90 515
6 90 83 92 84 88 87 524
7 85 92 84 89 81 90 521
8 88 86 90 83 85 88 520
9 92 87 85 90 88 91 533
10 78 84 87 90 92 86 517
11 84 85 89 92 87 83 520
12 90 89 92 87 85 88 521
13 85 82 86 91 88 90 522
14 87 90 91 83 88 86 525
15 92 87 89 85 86 90 529
16 85 90 88 91 87 89 530
17 83 82 85 89 91 87 517
18 89 91 86 90 88 85 529
19 91 85 88 89 90 86 529
20 87 90 82 88 85 91 523
21 88 89 85 92 87 90 531
22 90 85 88 91 87 89 530
23 87 92 89 85 90 86 529
24 85 90 88 91 87 89 530
25 92 87 85 90 88 91 533
26 90 85 88 91 87 89 530
27 87 90 82 88 85 91 523
28 88 89 85 92 87 90 531
29 91 85 88 89 90 86 529
30 87 90 89 85 86 90 527
31 85 90 88 91 87 89 530
32 83 82 85 89 91 87 517
33 89 91 86 90 88 85 529
34 78 84 87 90 92 86 517
35 84 85 89 92 87 83 520
36 90 89 92 87 85 88 521
37 85 82 86 91 88 90 522
38 87 90 91 83 88 86 525
39 92 87 89 85 86 90 529
40 85 90 88 91 87 89 530
41 83 82 85 89 91 87 517
42 89 91 86 90 88 85 529
43 91 85 88 89 90 86 529
44 87 90 82 88 85 91 523
45 88 89 85 92 87 90 531
平均分 87.71 87.04 88.16 88.47 87.53 88.02
```
阅读全文