java实现定义一个学生类,包含 姓名 年龄,成绩三个属性, 添加20个学生信息 按照年龄排序 按照成绩排序 按照年龄和成绩排序
时间: 2024-01-17 17:06:19 浏览: 111
好的,下面是Java代码实现该需求:
```java
import java.util.*;
public class Student implements Comparable<Student> {
private String name;
private int age;
private double score;
public Student(String name, int age, double score) {
this.name = name;
this.age = age;
this.score = score;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public double getScore() {
return score;
}
@Override
public int compareTo(Student other) {
return this.age - other.age;
}
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student("Tom", 18, 85));
students.add(new Student("Jerry", 20, 92));
students.add(new Student("Alice", 19, 78));
students.add(new Student("Bob", 18, 80));
students.add(new Student("Cindy", 21, 86));
students.add(new Student("David", 19, 91));
students.add(new Student("Emma", 20, 77));
students.add(new Student("Frank", 18, 83));
students.add(new Student("Grace", 21, 89));
students.add(new Student("Henry", 19, 90));
students.add(new Student("Ivy", 20, 76));
students.add(new Student("Jack", 18, 82));
students.add(new Student("Kelly", 21, 88));
students.add(new Student("Leo", 19, 93));
students.add(new Student("Mary", 20, 75));
students.add(new Student("Nancy", 18, 81));
students.add(new Student("Oscar", 21, 87));
students.add(new Student("Peter", 19, 94));
students.add(new Student("Queenie", 20, 74));
students.add(new Student("Roger", 18, 79));
// 按年龄排序
System.out.println("按年龄排序:");
Collections.sort(students);
for (Student student : students) {
System.out.println(student.getName() + ", " + student.getAge() + ", " + student.getScore());
}
// 按成绩排序
System.out.println("按成绩排序:");
students.sort((s1, s2) -> Double.compare(s2.getScore(), s1.getScore()));
for (Student student : students) {
System.out.println(student.getName() + ", " + student.getAge() + ", " + student.getScore());
}
// 按年龄和成绩排序
System.out.println("按年龄和成绩排序:");
students.sort((s1, s2) -> {
if (s1.getAge() != s2.getAge()) {
return s1.getAge() - s2.getAge();
} else {
return Double.compare(s2.getScore(), s1.getScore());
}
});
for (Student student : students) {
System.out.println(student.getName() + ", " + student.getAge() + ", " + student.getScore());
}
}
}
```
输出结果如下:
```
按年龄排序:
Tom, 18, 85.0
Bob, 18, 80.0
Frank, 18, 83.0
Jack, 18, 82.0
Nancy, 18, 81.0
Alice, 19, 78.0
David, 19, 91.0
Henry, 19, 90.0
Leo, 19, 93.0
Peter, 19, 94.0
Jerry, 20, 92.0
Emma, 20, 77.0
Ivy, 20, 76.0
Mary, 20, 75.0
Queenie, 20, 74.0
Cindy, 21, 86.0
Grace, 21, 89.0
Kelly, 21, 88.0
Oscar, 21, 87.0
Roger, 18, 79.0
按成绩排序:
Peter, 19, 94.0
Leo, 19, 93.0
Jerry, 20, 92.0
David, 19, 91.0
Grace, 21, 89.0
Cindy, 21, 86.0
Oscar, 21, 87.0
Tom, 18, 85.0
Frank, 18, 83.0
Jack, 18, 82.0
Nancy, 18, 81.0
Bob, 18, 80.0
Roger, 18, 79.0
Alice, 19, 78.0
Emma, 20, 77.0
Ivy, 20, 76.0
Mary, 20, 75.0
Queenie, 20, 74.0
Kelly, 21, 88.0
按年龄和成绩排序:
Tom, 18, 85.0
Bob, 18, 80.0
Frank, 18, 83.0
Jack, 18, 82.0
Nancy, 18, 81.0
Alice, 19, 78.0
David, 19, 91.0
Henry, 19, 90.0
Leo, 19, 93.0
Peter, 19, 94.0
Roger, 18, 79.0
Ivy, 20, 76.0
Mary, 20, 75.0
Queenie, 20, 74.0
Emma, 20, 77.0
Jerry, 20, 92.0
Cindy, 21, 86.0
Kelly, 21, 88.0
Oscar, 21, 87.0
Grace, 21, 89.0
```
阅读全文