- //键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低输出到控制台。
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入学生成绩:姓名,语文成绩,数学成绩,英语成绩");
- TreeSet<Person> ts = new TreeSet<>(new Comparator<Person>() {
- @Override
- public int compare(Person p1, Person p2) {
- int num =p2.getSum() - p1.getSum();
- return num == 0 ? 1 : num ;
-
- }
- });
-
- while(ts.size() < 5) {
- String line = sc.nextLine();
- String[] arr = line.split(",");
- int chinese = Integer.parseInt(arr[1]);
- int math = Integer.parseInt(arr[2]);
- int english = Integer.parseInt(arr[3]);
- ts.add(new Person(arr[0], chinese, math, english));
- }
- System.out.println("排序后的学生信息:");
- for (Person p : ts) {
- System.out.println(p);
- }
- }
复制代码 |
|