输入20名同学的学号、姓名、三门课程成绩; 计算每位同学的总分与平均分; 输出成绩表、显示总分最高分与最低分学生的成绩信息。JAVA语言
时间: 2024-11-05 10:23:50 浏览: 38
在Java中,你可以使用ArrayList或HashMap来存储学生的信息,并遍历数据结构来计算总分和平均分。这里是一个简单的示例代码:
```java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
class Student {
String id;
String name;
double score1;
double score2;
double score3;
// 构造函数和 getters / setters 省略
}
public class Main {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
// 添加20位同学的数据
for (int i = 0; i < 20; i++) {
Student student = new Student();
student.id = "S" + (i+1);
student.name = "Student " + (i+1);
student.score1 = getRandomScore(); // 模拟生成随机分数
student.score2 = getRandomScore();
student.score3 = getRandomScore();
students.add(student);
}
Map<String, Double[]> results = calculateScores(students);
printScoreTable(results);
double maxScore = Collections.max(results.values(), Comparator.comparingDouble(a -> a[0])); // 总分最大值
double minScore = Collections.min(results.values(), Comparator.comparingDouble(a -> a[0])); // 总分最小值
System.out.println("最高分学生成绩信息: 学号: " + results.entrySet().stream()
.filter(e -> e.getValue()[0] == maxScore)
.findFirst().orElse(null).getKey() + ", 总分: " + maxScore);
System.out.println("最低分学生成绩信息: 学号: " + results.entrySet().stream()
.filter(e -> e.getValue()[0] == minScore)
.findFirst().orElse(null).getKey() + ", 总分: " + minScore);
}
private static Map<String, Double[]> calculateScores(ArrayList<Student> students) {
Map<String, Double[]> scoresMap = new HashMap<>();
for (Student student : students) {
Double[] scoreArray = {student.score1 + student.score2 + student.score3};
scoresMap.put(student.id, scoreArray);
}
return scoresMap;
}
private static double getRandomScore() {
return Math.random() * 100; // 生成0到100之间的随机数作为成绩
}
private static void printScoreTable(Map<String, Double[]> results) {
System.out.println("学号\t姓名\t总分");
results.forEach((id, scoreArray) -> {
System.out.printf("%s\t%s\t%.2f\n", id, results.get(id)[0] > 90 ? "优秀" : "良好", scoreArray[0]);
});
}
}
```
这个程序首先创建了20个学生对象,然后计算每个学生的总分并存储在一个HashMap中。接着,它打印出成绩表并找到总分的最高分和最低分的学生信息。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044947.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)