用java写输入 测试数据有多组,每组输入第一行有一个整数N(N<=100),接下来的N行包括N个学生的数据。每个学生的数据包括姓名(长度不超过100的字符串)、年龄(整形数)、成绩(小于等于100的正数)。 输出 将学生信息按成绩进行排序,成绩相同的则按姓名的字母序进行排序。然后输出学生信息,按照如下格式:姓名 年龄 成绩
时间: 2024-03-14 16:48:30 浏览: 46
JAVA 输入学生成绩后总成绩排序输出
好的,以下是示例代码,供您参考:
```java
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int n = scanner.nextInt();
Student[] students = new Student[n];
for (int i = 0; i < n; i++) {
String name = scanner.next();
int age = scanner.nextInt();
int score = scanner.nextInt();
students[i] = new Student(name, age, score);
}
Arrays.sort(students, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
if (o1.score != o2.score) {
return o2.score - o1.score;
} else {
return o1.name.compareTo(o2.name);
}
}
});
for (Student student : students) {
System.out.println(student.name + " " + student.age + " " + student.score);
}
}
}
private static class Student {
public String name;
public int age;
public int score;
public Student(String name, int age, int score) {
this.name = name;
this.age = age;
this.score = score;
}
}
}
```
输入样例:
```
3
Tom 18 80
Mike 20 90
Jack 19 80
```
输出样例:
```
Mike 20 90
Jack 19 80
Tom 18 80
```
阅读全文