用java把三个成绩的总成绩排序_用java编一个学生成绩排序成绩 有学生的姓名 学号 三门课的成绩 然后对总分排序...
时间: 2024-03-23 15:37:02 浏览: 15
好的,这是一个简单的Java代码示例,可以根据学生的总分对成绩进行排序。
```
import java.util.*;
class Student implements Comparable<Student>{
String name;
int id;
int score1;
int score2;
int 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 String toString() {
return name + " " + id + " " + score1 + " " + score2 + " " + score3 + " " + totalScore;
}
public int compareTo(Student other) {
return Integer.compare(other.totalScore, this.totalScore);
}
}
public class ScoreSorter {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student("Tom", 1, 80, 90, 70));
students.add(new Student("Jack", 2, 90, 80, 85));
students.add(new Student("Mary", 3, 75, 80, 90));
Collections.sort(students);
for(Student student : students) {
System.out.println(student);
}
}
}
```
首先,我们定义了一个名为`Student`的类,它有学生的姓名、学号和三门课的成绩。我们还添加了一个`totalScore`属性,用于计算学生的总分。然后,我们实现了`Comparable`接口,以便我们可以将学生对象按照总分进行排序。
在`main`方法中,我们创建了一个`Student`对象列表,并通过`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_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)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)