JAVA键盘录入15个学生信息(学号,姓名,语文成绩,数学成绩,英语成绩)按照总分从高到低输出
时间: 2023-08-24 17:07:23 浏览: 124
JAVA 键盘录入学生成绩,排序后输出
好的,以下是JAVA代码实现:
```java
import java.util.Scanner;
import java.util.Arrays;
public class Student {
int id;
String name;
int chineseGrade;
int mathGrade;
int englishGrade;
int totalGrade;
public Student(int id, String name, int chineseGrade, int mathGrade, int englishGrade) {
this.id = id;
this.name = name;
this.chineseGrade = chineseGrade;
this.mathGrade = mathGrade;
this.englishGrade = englishGrade;
this.totalGrade = chineseGrade + mathGrade + englishGrade;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Student[] students = new Student[15];
// 录入学生信息
for (int i = 0; i < students.length; i++) {
System.out.println("请输入第" + (i+1) + "个学生的信息:");
System.out.print("学号:");
int id = scanner.nextInt();
System.out.print("姓名:");
String name = scanner.next();
System.out.print("语文成绩:");
int chineseGrade = scanner.nextInt();
System.out.print("数学成绩:");
int mathGrade = scanner.nextInt();
System.out.print("英语成绩:");
int englishGrade = scanner.nextInt();
students[i] = new Student(id, name, chineseGrade, mathGrade, englishGrade);
}
// 按总分从高到低排序
Arrays.sort(students, (a, b) -> b.totalGrade - a.totalGrade);
// 输出学生信息
System.out.println("按总分从高到低排序的学生信息如下:");
for (int i = 0; i < students.length; i++) {
System.out.println("学号:" + students[i].id +
" 姓名:" + students[i].name +
" 语文成绩:" + students[i].chineseGrade +
" 数学成绩:" + students[i].mathGrade +
" 英语成绩:" + students[i].englishGrade +
" 总分:" + students[i].totalGrade);
}
}
}
```
运行代码,会依次让你输入15个学生的学号、姓名、语文成绩、数学成绩和英语成绩,然后按照总分从高到低输出学生信息。
阅读全文