用java把三个成绩的总成绩排序_用java编一个学生成绩排序成绩 有学生的姓名 学号 三门课的成绩 然后对总分排序...
时间: 2024-03-24 19:41:30 浏览: 22
好的,下面是使用Java编写一个学生成绩排序的程序,该程序包括学生的姓名、学号和三门课程的成绩,并按照总分进行排序:
```java
import java.util.*;
class Student implements Comparable<Student> {
private String name;
private String id;
private double score1;
private double score2;
private double score3;
private double totalScore;
public Student(String name, String id, double score1, double score2, double score3) {
this.name = name;
this.id = id;
this.score1 = score1;
this.score2 = score2;
this.score3 = score3;
this.totalScore = score1 + score2 + score3;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public double getScore1() {
return score1;
}
public double getScore2() {
return score2;
}
public double getScore3() {
return score3;
}
public double getTotalScore() {
return totalScore;
}
@Override
public int compareTo(Student other) {
if (this.totalScore > other.totalScore) {
return -1;
} else if (this.totalScore < other.totalScore) {
return 1;
} else {
return 0;
}
}
}
public class SortStudents {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student("Alice", "001", 80.0, 90.0, 85.0));
students.add(new Student("Bob", "002", 75.0, 85.0, 90.0));
students.add(new Student("Charlie", "003", 90.0, 80.0, 75.0));
Collections.sort(students);
for (Student student : students) {
System.out.println(student.getName() + " " + student.getId() + " " + student.getTotalScore());
}
}
}
```
以上程序中,`Student`类包括学生的基本信息以及三门课的成绩,还包括计算总分的方法。为了能够对学生按照总分进行排序,`Student`类实现了`Comparable`接口,并重写了`compareTo`方法。`SortStudents`类创建了一个包含若干个`Student`对象的List,并使用`Collections.sort`方法对其进行排序。最后,程序输出排序后的学生信息,包括姓名、学号和总分。
阅读全文