题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
import java.util.Scanner; import java.util.LinkedList; class Student { String name; Integer score; public Student(String name, Integer score) { this.name = name; this.score = score; } } // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int num = Integer.parseInt(in.nextLine()); int sort = Integer.parseInt(in.nextLine()); LinkedList<Student> students = new LinkedList<>(); for (int i = 0; i < num; i++) { String[] split = in.nextLine().split(" "); students.add(new Student(split[0], Integer.parseInt(split[1]))); } if (sort == 0) students.sort(((o1, o2) -> o2.score - o1.score)); else students.sort(((o1, o2) -> o1.score - o2.score)); for (Student student : students) { System.out.println(student.name + " " + student.score); } } }