用java把三个成绩的总成绩排序_用java编一个学生成绩排序成绩 有学生的姓名 学号 三门课的成绩 然后对总分排序...
时间: 2024-03-01 22:55:57 浏览: 20
好的,这是一个简单的Java程序,用于对学生的总成绩进行排序:
```java
import java.util.*;
class Student implements Comparable<Student> {
String name;
int id;
int score1, score2, score3;
int totalScore;
public Student(String name, int id, int score1, int score2, int score3) {
this.name = name;
this.id = id;
this.score1 = score1;
this.score2 = score2;
this.score3 = score3;
this.totalScore = score1 + score2 + score3;
}
public int compareTo(Student s) {
return s.totalScore - this.totalScore;
}
public String toString() {
return this.name + " " + this.id + " " + this.score1 + " " + this.score2 + " " + this.score3 + " " + this.totalScore;
}
}
public class SortStudents {
public static void main(String[] args) {
List<Student> students = new ArrayList<Student>();
students.add(new Student("Alice", 1, 80, 90, 70));
students.add(new Student("Bob", 2, 90, 70, 80));
students.add(new Student("Charlie", 3, 70, 80, 90));
Collections.sort(students);
for (Student s : students) {
System.out.println(s);
}
}
}
```
首先,我们定义了一个`Student`类,它有五个属性:`name`(姓名)、`id`(学号)、`score1`、`score2`、`score3`(三门课的成绩)和`totalScore`(总成绩)。在构造函数中,我们计算了总成绩。
然后,我们实现了`Comparable`接口,以便可以使用`Collections.sort()`方法对学生进行排序。我们按照学生的总成绩进行排序,从高到低。
最后,我们在`main()`函数中创建了三个学生对象,并将它们添加到一个`List`中。然后,我们使用`Collections.sort()`方法对学生进行排序,并打印排序后的结果。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)