j和j+1交换不是和i交换
import java.util.Scanner;
public class Student {
private String name;
private String stuId;
private int[] scores;
private double avgScore;
public Student(String name, String stuId, int[] scores) {
this.name = name;
this.stuId = stuId;
this.scores = scores;
double totalScore = 0;
for (int score : scores) {
totalScore += score;
}
this.avgScore = totalScore / scores.length;
}
public String getName() {
return name;
}
public String getStuId() {
return stuId;
}
public int[] getScores() {
return scores;
}
public double getAvgScore() {
return avgScore;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入学生的个数:");
int n = scanner.nextInt();
scanner.nextLine();
Student[] students = new Student[n];
for (int i = 0; i < n; i++) {
System.out.print("请输入第" + (i+1) + "个学生的名字:");
String name = scanner.nextLine();
System.out.print("请输入第" + (i+1) + "个学生的学号:");
String stuId = scanner.nextLine();
System.out.print("请输入第" + (i+1) + "个学生的成绩(以空格分隔):");
String[] scoreStrArr = scanner.nextLine().split(" ");
int[] scores = new int[scoreStrArr.length];
for (int j = 0; j < scores.length; j++) {
scores[j] = Integer.parseInt(scoreStrArr[j]);
}
students[i] = new Student(name, stuId, scores);
}
// 计算每门学科的平均成绩
double[] subjectAvgScores = new double[students[0].getScores().length];
for (int i = 0; i < subjectAvgScores.length; i++) {
double totalScore = 0;
for (Student student : students) {
totalScore += student.getScores()[i];
}
subjectAvgScores[i] = totalScore / n;
}
System.out.println("每门学科的平均成绩为:");
for (int i = 0; i < subjectAvgScores.length; i++) {
System.out.print(subjectAvgScores[i] + " ");
}
System.out.println();
// 计算每位同学的平均成绩,并按学号由大到小排序
System.out.println("按学号由大到小排序的每位同学的平均成绩为:");
for (int i = 0; i < students.length; i++) {
for (int j = 0; j < students.length-i-1; j++) {
if (students[j].getStuId().compareTo(students[j+1].getStuId()) < 0) {
Student temp = students[j];
students[j] = students[j+1];
students[j+1] = temp;
}
}
}
for (Student student : students) {
System.out.println(student.getName() + " " + student.getStuId() + " " + student.getAvgScore());
}
// 按总成绩由大到小排序
System.out.println("按总成绩由大到小排序的所有学生信息为:");
for (int i = 0; i < students.length; i++) {
for (int j = 0; j < students.length-i-1; j++) {
double score1 = 0;
for (int k = 0; k < students[j].getScores().length; k++) {
score1 += students[j].getScores()[k];
}
double score2 = 0;
for (int k = 0; k < students[j+1].getScores().length; k++) {
score2 += students[j+1].getScores()[k];
}
if (score1 < score2) {
Student temp = students[j];
students[j] = students[j+1];
students[j+1] = temp;
}
}
}
for (Student student : students) {
System.out.println(student.getName() + " " + student.getStuId() + " " + student.getAvgScore());
}
}
}
你不能只交换学生结构,names中的对应元素也要交换啊。这样才能保持一一对应的关系
if (subjectAverages[i] < subjectAverages[j]) {
t = students[i];
students[i] = students[j];
students[j] = t;
float avg = subjectAverages[i];
subjectAverages[i] = subjectAverages[j];
subjectAverages[j] = avg;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!