题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static class Student{ private String name; private int score; Student(){}; Student(String name,int score){ this.name=name; this.score=score; } } public static void main(String[] args) { Scanner in = new Scanner(System.in); List<Student> list =new ArrayList<>(); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case int a = in.nextInt(); int b = in.nextInt(); for (int i = 0; i < a; i++) { String name = in.next(); int score= in.nextInt(); list.add(new Student(name,score)); } if (b==0){ list.sort(new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { return o2.score-o1.score; } }); }else { list.sort(new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { return o1.score-o2.score; } }); } list.forEach(res-> System.out.println(res.name+" "+res.score)); } } }