java编写一个程序:第一行输入一个整数n表示学生人数,以下n行包含一个字符串和三个整数,分别代表学生的姓名,中文成绩,英语成绩和数学成绩。按照学生的姓名顺序(首字母排序方式)排列。(n介于1至200之间,且分数介于1至100之间)
时间: 2023-06-19 17:07:41 浏览: 87
以下是 Java 程序的代码实现:
```java
import java.util.*;
public class StudentScoreSorter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
List<Student> students = new ArrayList<>();
for (int i = 0; i < n; i++) {
String name = scanner.next();
int chineseScore = scanner.nextInt();
int englishScore = scanner.nextInt();
int mathScore = scanner.nextInt();
students.add(new Student(name, chineseScore, englishScore, mathScore));
}
Collections.sort(students);
for (Student student : students) {
System.out.println(student.name + " " + student.chineseScore + " " + student.englishScore + " " + student.mathScore);
}
}
}
class Student implements Comparable<Student> {
String name;
int chineseScore;
int englishScore;
int mathScore;
public Student(String name, int chineseScore, int englishScore, int mathScore) {
this.name = name;
this.chineseScore = chineseScore;
this.englishScore = englishScore;
this.mathScore = mathScore;
}
@Override
public int compareTo(Student other) {
return this.name.compareTo(other.name);
}
}
```
该程序首先读入学生人数n,然后读入每个学生的姓名和三个成绩,将每个学生封装成一个 Student 对象,并添加到一个 List 中。接着使用 Collections.sort() 方法对 List 中的 Student 对象进行排序,排序方式为按照学生的姓名顺序(首字母排序方式)排列。最后遍历排序后的 List,输出每个学生的姓名和三个成绩。
阅读全文