248 成绩排序

248 成绩排序

【需求】

用存储多个学生的信息(语文、数学分数)并遍历

要求,按分数从高到低排序输出

这里使用比较器排序

自己用自然排序做练习

【代码思路】

- 定义学生类

- 创建TreeSet集合对象,通过比较器排序进行排序

- 创建学生对象

- 添加学生对象到集合

- 遍历集合,按分数从高到低输出

【代码过程事件】

如果不做以下处理

@Override

            public int compare(Student248 s1, Student248 s2) {

//                int num1 = (s2.getChineseScore() + s2.getMathScore())-(s1.getChineseScore() - s1.getMathScore());

                int num2 = s2.getScoreSum() - s1.getScoreSum();//主要条件

                int num3 = num2==0?s1.getChineseScore() - s1.getChineseScore() : num2;//次要条件

                int num4 = num3==0?s1.getName().compareTo(s2.getName()) : num3;

                return num4;

            }

系统将无法添加总分相同的学生,即使姓名不同、各科分数不同

--------------------------------------------------------------

1 package e248;

2

3 import java.util.Comparator;

4 import java.util.TreeSet;

5

6 public class TreeSetDemo248 {

7     public static void main(String[] args) {

8         TreeSet<Student248> ts248 = new TreeSet<Student248>(new Comparator<Student248>() {

9             @Override

10             public int compare(Student248 s1, Student248 s2) {

11 //                int num1 = (s2.getChineseScore() + s2.getMathScore())-(s1.getChineseScore() - s1.getMathScore());

12                 int num2 = s2.getScoreSum() - s1.getScoreSum();

13                 int num3 = num2==0?s1.getChineseScore() - s1.getChineseScore() : num2;

14                 int num4 = num3==0?s1.getName().compareTo(s2.getName()) : num3;

15                 return num4;

16             }

17         });//如果学科多,用num2更好

18         //num2,在Student248里定义个方法,计算总成绩,调用时直接调用总成绩

19         Student248 zz1 = new Student248("AVA",7,7);

20         Student248 zz2 = new Student248("MOLE",2,2);

21         Student248 zz3 = new Student248("RAT",8,8);

22         Student248 zz4 = new Student248("TOAD",1,1);

23         Student248 zz5 = new Student248("BADGER",8,8);

24

25         ts248.add(zz1);

26         ts248.add(zz2);

27         ts248.add(zz3);

28         ts248.add(zz4);

29         ts248.add(zz5);

30

31         System.out.println("---29---");

32         for (Student248 s248 : ts248 ) {

33             System.out.print(s248.getName()+","+s248.getMathScore()+","+s248.getChineseScore());

34             System.out.println(", and in total:"+s248.getScoreSum());

35         }

36

37     }

38 }

--------------------------------------------------------------

1 package e248;

2

3 public class Student248 {

4     private String name;

5     private int ChineseScore;

6     private int MathScore;

7

8     public Student248() {

9     }

10

11     public Student248(String name, int chineseScore, int mathScore) {

12         this.name = name;

13         ChineseScore = chineseScore;

14         MathScore = mathScore;

15     }

16

17     public String getName() {

18         return name;

19     }

20

21     public void setName(String name) {

22         this.name = name;

23     }

24

25     public int getChineseScore() {

26         return ChineseScore;

27     }

28

29     public void setChineseScore(int chineseScore) {

30         ChineseScore = chineseScore;

31     }

32

33     public int getMathScore() {

34         return MathScore;

35     }

36

37     public void setMathScore(int mathScore) {

38         MathScore = mathScore;

39     }

40     public int getScoreSum(){

41         return this.getChineseScore() + this.getMathScore();

42     }

43 }

猜你喜欢

转载自blog.csdn.net/m0_63673788/article/details/121397320