java 17 - 9 HashSet集合、TreeSet集合的练习


TreeSet集合的练习:

  需求: 键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低输出到控制台 

  分析:
    A:定义学生类
    B:创建一个TreeSet集合 使用内部匿名方法实现
    C:总分从高到底如何实现呢?
    D:键盘录入5个学生信息
    E:遍历TreeSet集合

 

学生类:

 1 package zl_TreeSetTest;
 2 /*
 3      创建学生类
 4      成员变量:
 5          A:姓名
 6          B:语文成绩   数学成绩  英语成绩   
 7  */
 8 public class Student {
 9         private String name ;
10         private int chinese;
11         private int english;
12         private int math;
13         
14         public Student() {
15             super();
16             // TODO Auto-generated constructor stub
17         }
18         
19         
20         @Override
21         public String toString() {
22             return "Student [name=" + name + ", chinese=" + chinese
23                     + ", english=" + english + ", math=" + math + "]";
24         }
25 
26 
27         public String getName() {
28             return name;
29         }
30         public void setName(String name) {
31             this.name = name;
32         }
33         public int getChinese() {
34             return chinese;
35         }
36         public void setChinese(int chinese) {
37             this.chinese = chinese;
38         }
39         public int getEnglish() {
40             return english;
41         }
42         public void setEnglish(int english) {
43             this.english = english;
44         }
45         public int getMath() {
46             return math;
47         }
48         public void setMath(int math) {
49             this.math = math;
50         }
51 
52         //按照总分从高到低输出到控制台
53         public int getsum (){
54             int sum = this.chinese + this.english + this.math;
55             return sum;
56         }
57         
58         
59         
60         
61 }

 

测试类:

 1 package zl_TreeSetTest;
 2 
 3 import java.util.Comparator;
 4 import java.util.Scanner;
 5 import java.util.TreeSet;
 6 
 7 /*
 8  键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低输出到控制台
 9 
10  分析:
11  A:定义学生类
12  B:创建一个TreeSet集合   使用内部匿名方法实现
13  C:总分从高到底如何实现呢?        
14  D:键盘录入5个学生信息
15  E:遍历TreeSet集合
16  */
17 public class StudentTest {
18 
19     public static void main(String[] args) {
20         // 创建一个TreeSet集合
21         TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
22             public int compare(Student s1, Student s2) {
23                 // 首先比较总分
24                 int num1 = s2.getsum() - s2.getsum();
25                 // 总分相同,但语文成绩未必相同
26                 int num2 = num1 == 0 ? s1.getChinese() - s2.getChinese() : num1;
27                 // 语文成绩也相同的话,但是英语成绩未必也相同
28                 int num3 = num2 == 0 ? s1.getEnglish() - s2.getEnglish() : num2;
29                 // 。。。数学成绩未必相同:
30                 int num4 = num3 == 0 ? s1.getMath() - s2.getMath() : num3;
31                 // 成绩方面都相同的话,但是你名字肯定不相同吧。。
32                 int num5 = num4 == 0 ? s1.getName().compareTo(s2.getName())
33                         : num4;
34 
35                 return num5;
36             }
37 
38         });
39 
40         System.out.println("--------录入学生信息--------");
41         for(int x = 1; x<= 5 ;x++) {
42             
43             // 键盘录入
44             Scanner sc = new Scanner(System.in);
45             
46             System.out.println("请输入第" + x + "位学生的名字:");
47             String name = sc.nextLine();
48             System.out.println("请输入第" + x + "位学生的语文成绩:");
49             int chinese = sc.nextInt();
50             System.out.println("请输入第" + x + "位学生的数学成绩:");
51             int Math = sc.nextInt();
52             System.out.println("请输入第" + x + "位学生的英语成绩:");
53             int english = sc.nextInt();
54 
55             // 把数据录入学生类中
56             Student s = new Student();
57             s.setName(name);
58             s.setChinese(chinese);
59             s.setMath(Math);
60             s.setEnglish(english);
61 
62             // 在把学生类放进集合中
63             ts.add(s);
64 
65         }
66 
67         // 进行遍历
68         for (Student s : ts) {
69             System.out.println(s.getChinese() + "\t" + s.getMath() + "\t"
70                     + s.getEnglish() + "\t" + s.getsum());
71         }
72 
73     }
74 
75 }

 

posted @ 2016-09-24 22:52  卡拉瓦  阅读(344)  评论(0编辑  收藏  举报