集合框架
学生成绩
录入几个学生信息 姓名, 语文, 数学,英语, 按照总分高低输出到控制台
public class Student {
//
private String name;
private int yw;
private int shuxue;
private int eng;
private int sum;
public Student(String name, int yw, int shuxue, int eng){
this.name = name;
this.yw = yw;
this.shuxue = shuxue;
this.eng = eng;
this.sum = yw + shuxue + eng;
}
public int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
@Override
public String toString() {
return "{" +
"name='" + name + '\'' +
", sum=" + sum +
'}';
}
}
---------------------------------
package com.zhou.y2;
import java.sql.Array;
import java.util.*;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入: 姓名, 语文成绩, 数学成绩, 英语成绩");
TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>(){
@Override
public int compare(Student s1, Student s2) {
int num = s2.getSum() - s1.getSum();
return num == 0? 1 : num;
}
});
while (ts.size() <= 2){
String str = sc.nextLine();
String[] arr = str.split(",");
Integer yu = Integer.parseInt(arr[1]);
Integer shu = Integer.parseInt(arr[2]);
Integer wai = Integer.parseInt(arr[3]);
ts.add(new Student(arr[0], yu, shu, wai));
}
for (Student s : ts){
System.out.println(s);
}
System.out.println(ts);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66