Java实现,输入这10个学生的成绩,然后计算这些学生的平均分数和最高分数,最后对这10个学生的成绩进行排序
时间: 2024-09-26 21:08:22 浏览: 252
在Java中,我们可以使用ArrayList来存储学生的成绩,并通过遍历数据结构来完成上述任务。这里是一个简单的示例:
import java.util.ArrayList;
import java.util.Collections;
class Student {
int score;
// 构造函数和getter/setter省略
}
public class Main {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
// 添加10个学生的成绩,例如:
for (int i = 0; i < 10; i++) {
Student student = new Student();
student.score = generateRandomScore(); // 假设generateRandomScore()生成随机数
students.add(student);
}
double sum = 0;
int maxScore = students.get(0).score; // 假定第一个学生是初始最大值
// 计算平均分和最高分
for (Student student : students) {
sum += student.score;
if (student.score > maxScore) {
maxScore = student.score;
}
}
double averageScore = sum / students.size();
System.out.println("平均分数: " + averageScore);
System.out.println("最高分数: " + maxScore);
// 对学生成绩进行排序
Collections.sort(students, (a, b) -> a.score - b.score); // 按照成绩从低到高排序
System.out.println("按成绩排序后的学生列表:");
for (Student student : students) {
System.out.println(student.score);
}
}
private static int generateRandomScore() {
return (int)(Math.random() * 100); // 生成0-99之间的随机整数
}
}
在这个例子中,我们首先创建了一个包含10个学生对象的ArrayList,然后计算了他们的总分、找出最高分,并对所有成绩进行了升序排序。
相关推荐


















