编写程序,统计班级同学的期末考试成绩,按总成绩高低进行排名。要求统计人数为10人,统计6门课程考试成绩,从键盘输入每个同学的姓名、学号及各门考试成绩,计算总成绩并排序后,输出每人的排名及总成绩。(程序中至少包含3个类java
时间: 2024-03-08 19:51:10 浏览: 102
程序用JAVA编写,统计每个学生的总分
好的,这是一个比较复杂的问题,需要用到面向对象编程的知识。以下是按照Java语言编写的程序,实现班级成绩排名的功能:
首先,我们可以定义一个Student类,用来存储每个同学的信息,包括姓名、学号、各门成绩和总成绩。代码如下:
```java
public class Student {
private String name;
private String id;
private int[] scores;
private int totalScore;
public Student(String name, String id, int[] scores) {
this.name = name;
this.id = id;
this.scores = scores;
this.totalScore = calculateTotalScore();
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public int[] getScores() {
return scores;
}
public int getTotalScore() {
return totalScore;
}
private int calculateTotalScore() {
int totalScore = 0;
for (int score : scores) {
totalScore += score;
}
return totalScore;
}
}
```
接下来,我们可以定义一个Classroom类,用来管理班级同学的信息,包括添加学生、计算总成绩排名等功能。代码如下:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Classroom {
private List<Student> students = new ArrayList<>();
public void addStudent(Student student) {
students.add(student);
}
public void sortStudentsByTotalScore() {
Collections.sort(students, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
return s2.getTotalScore() - s1.getTotalScore();
}
});
}
public void printRanking() {
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
System.out.println("第" + (i + 1) + "名:" + student.getName() + ",总成绩:" + student.getTotalScore());
}
}
}
```
最后,我们可以编写一个主程序,用来输入每个同学的信息,计算总成绩排名并输出结果。代码如下:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Classroom classroom = new Classroom();
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 10; i++) {
System.out.print("请输入第" + (i + 1) + "个同学的姓名:");
String name = scanner.next();
System.out.print("请输入第" + (i + 1) + "个同学的学号:");
String id = scanner.next();
int[] scores = new int[6];
for (int j = 0; j < 6; j++) {
System.out.print("请输入第" + (i + 1) + "个同学的第" + (j + 1) + "门课程成绩:");
scores[j] = scanner.nextInt();
}
Student student = new Student(name, id, scores);
classroom.addStudent(student);
}
classroom.sortStudentsByTotalScore();
classroom.printRanking();
}
}
```
这样,我们就完成了一个简单的班级成绩排名程序。请注意,本程序只是一个简单的示例,实际应用中可能需要更多的功能和优化。
阅读全文