信息学院评定奖学金,需要对整个年级的学生按照平均分数进行排名 //根据输入的学号和平均成绩,按照平均成绩降序输出学号,若平均成绩相同,按照学号顺序输出

 1 //信息学院评定奖学金,需要对整个年级的学生按照平均分数进行排名
 2 //根据输入的学号和平均成绩,按照平均成绩降序输出学号,若平均成绩相同,按照学号顺序输出
 3 
 4 package classwork9;
 5 
 6 public class Student implements Comparable<Student> {
 7         private String id;
 8         private String name;
 9         private int score;
10         public Student() {
11         }
12         public Student(String id, String name, int score) {
13             this.id = id;
14             this.name = name;
15             this.score = score;
16         }
17         public String getId() {
18             return id;
19         }
20         public String getName() {
21             return name;
22         }
23         public int getScore() {
24             return score;
25         }
26         public void setId(String id) {
27             this.id = id;
28         }
29         public void setName(String name) {
30             this.name = name;
31         }
32         public void setScore(int score) {
33             this.score = score;
34         }
35         @Override
36         public String toString() {
37             return "[id=" + id + ", name=" + name + ", score=" + score + "]";
38         }
39         public int compareTo(Student student) {
40             int result=student.score-this.score;
41             if(result==0) {
42                 result=this.id.compareTo(student.id);
43             }
44             return result ;
45         }
46         
47 }
48 
49 
50 package classwork9;
51 
52 import java.util.Set;
53 import java.util.TreeSet;
54 
55 public class Pdjxj {
56 
57     public static void main(String[] args) {
58         Set<Student> a=new TreeSet<Student>();
59         Student student1 = new Student("2019216666", "小明", 97);
60         Student student2 = new Student("2019216676", "小红", 98);
61         Student student3 = new Student("2019216645", "小刚", 89);
62         Student student4 = new Student("2019216655", "小傻", 97);
63         a.add(student1);
64         a.add(student2);
65         a.add(student3);
66         a.add(student4);
67         for(Student x: a) {
68             System.out.println(x);
69         }
70      }
71 
72 }

 

posted @ 2020-12-13 14:01  丁帅帅dss  阅读(218)  评论(0)    收藏  举报