javase 学生管理系统 并具有成绩排序功能
时间: 2024-02-11 12:24:36 浏览: 38
你可以使用JavaSE开发一个学生管理系统,并在其中添加成绩排序功能。下面是一个简单的示例代码:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class Student {
private String name;
private int score;
public Student(String name, int score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public int getScore() {
return score;
}
}
class ScoreComparator implements Comparator<Student> {
@Override
public int compare(Student s1, Student s2) {
return s2.getScore() - s1.getScore();
}
}
public class StudentManagementSystem {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student("Alice", 80));
students.add(new Student("Bob", 90));
students.add(new Student("Charlie", 70));
System.out.println("排序前的学生列表:");
for (Student student : students) {
System.out.println(student.getName() + " - " + student.getScore());
}
Collections.sort(students, new ScoreComparator());
System.out.println("按成绩降序排序后的学生列表:");
for (Student student : students) {
System.out.println(student.getName() + " - " + student.getScore());
}
}
}
```
这段代码创建了一个`Student`类来表示学生,包含姓名和分数属性。然后定义了一个`ScoreComparator`类来实现比较器接口,用于按照成绩降序排序学生列表。在`main`方法中,创建了一个学生列表并添加了几个学生对象。然后使用`Collections.sort()`方法和`ScoreComparator`比较器对学生列表进行排序,并打印排序后的结果。
这只是一个简单的示例,你可以根据自己的需求进行扩展和修改。希望对你有帮助!