解题思路: 利用集合的sort进行排序(重写比较器)
注意事项: 第一排序条件为成绩 其次姓名 最后年龄
参考代码:
import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Scanner; /** * Main */ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); List<Student> students = new ArrayList<>(); for (int i = 0; i < n; i++) { String name = sc.next(); int age = sc.nextInt(); int score = sc.nextInt(); students.add(new Student(name, age, score)); } // 对list进行排序:调用list的sort并定义比较规则 students.sort((s1, s2) -> { if (s1.score != s2.score) { // 成绩不一样 return Integer.compare(s1.score, s2.score); } else if (!s1.name.equals(s2.name)) { // 成绩不一样,姓名不一样 return s1.name.compareTo(s2.name); } else { // 成绩、姓名都一样 return Integer.compare(s1.age, s2.age); } }); // 遍历输出 for (Student s : students) { System.out.println(s.name + " " + s.age + " " + s.score); } } sc.close(); } } class Student{ String name; int age; int score; public Student(String name, int age, int score){ this.name = name; this.age = age; this.score = score; } }
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题9.6 (C语言代码)浏览:589 |
C语言程序设计教程(第三版)课后习题7.2 (Java代码)浏览:688 |
C语言训练-列出最简真分数序列* (C语言代码)浏览:527 |
printf基础练习2 (C语言代码)浏览:308 |
Pascal三角 (C语言代码)格式错误浏览:534 |
C语言程序设计教程(第三版)课后习题8.2 (C语言代码)浏览:5241 |
简单的a+b (C语言代码)浏览:642 |
C语言程序设计教程(第三版)课后习题7.1 (C语言代码)浏览:626 |
大家好,我是验题君浏览:580 |
大神老白 (C语言代码)浏览:610 |