学生类成绩管理v1.0 描述: 按照输入输出格式要求,根据程序模板将代码补充完整。求出Student数组中 学生的最高平均分和最低平均分,并从高分到低分对数组重新排序,输出排序后的学生信息。 输入 先输入3个课程号,然后逐次输入4位学生的信息。 输出 按照格式输出最高分,最低分,以及排序后的结果。
时间: 2024-02-20 13:58:55 浏览: 100
JAVA课程设计 学生成绩管理系统 代码规整 格式规范
下面是一个可供参考的 Java 代码实现:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] courseNames = new String[3];
// 读入3门课程的名称
for (int i = 0; i < courseNames.length; i++) {
courseNames[i] = scanner.nextLine();
}
Student[] students = new Student[4];
// 读入4个学生的信息
for (int i = 0; i < students.length; i++) {
String name = scanner.next();
String id = scanner.next();
int[] scores = new int[3];
for (int j = 0; j < scores.length; j++) {
scores[j] = scanner.nextInt();
}
students[i] = new Student(name, id, scores);
}
// 计算每个学生的平均分并找出最高分和最低分
double maxAvg = Double.MIN_VALUE;
double minAvg = Double.MAX_VALUE;
for (Student student : students) {
double avg = student.getAverage();
if (avg > maxAvg) {
maxAvg = avg;
}
if (avg < minAvg) {
minAvg = avg;
}
}
// 排序
quickSort(students, 0, students.length - 1);
// 输出结果
System.out.printf("highest score=%.2f\n", maxAvg);
System.out.printf("lowest score=%.2f\n", minAvg);
for (Student student : students) {
System.out.println(student.toString());
}
}
// 快速排序
private static void quickSort(Student[] students, int left, int right) {
if (left >= right) {
return;
}
int i = left, j = right;
double pivot = students[(left + right) / 2].getAverage();
while (i <= j) {
while (students[i].getAverage() > pivot) {
i++;
}
while (students[j].getAverage() < pivot) {
j--;
}
if (i <= j) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
i++;
j--;
}
}
quickSort(students, left, j);
quickSort(students, i, right);
}
}
class Student {
private String name;
private String id;
private int[] scores;
private double average;
public Student(String name, String id, int[] scores) {
this.name = name;
this.id = id;
this.scores = scores;
this.average = calculateAverage();
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public int[] getScores() {
return scores;
}
public double getAverage() {
return average;
}
private double calculateAverage() {
int sum = 0;
for (int score : scores) {
sum += score;
}
return (double) sum / scores.length;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(name).append(" ").append(id).append(" ");
for (int score : scores) {
sb.append(score).append(" ");
}
sb.append(String.format("%.2f", average));
return sb.toString();
}
}
```
注意事项:
1. 学生类中需要计算平均分并提供访问器方法。
2. 输出平均分时需要保留2位小数。
3. 排序时需要指定比较的属性,此处为平均分。
阅读全文