酷代码 AI
菜单
服务商
DeepSeek字节豆包讯飞星火
更多选项

Java 实训:用直接插入与冒泡排序实现学生成绩复杂排名需求

以下是使用 Java 实现上述排序需求的代码示例: ```java import java.util.Arrays; class Student { int id; String name; int totalScore; int mathScore; int englishScore; Student(int id, String name, int totalScore, int mathScore, int englishScore) { this.id = id; this.name = name; this.totalScore = totalScore; this.mathScore = mathScore; this.englishScore = englishScore; } } public class SortingStudents { public static void main(String[] args) { Student[] students = { new Student(1, "Alice", 85, 90, 80), new Student(2, "Bob", 90, 85, 85), new Student(3, "Charlie", 85, 90, 85), new Student(4, "David", 90, 90, 80) }; Arrays.sort(students, (s1, s2) -> { if (s1.totalScore!= s2.totalScore) { return s2.totalScore - s1.totalScore; } else if (s1.mathScore!= s2.mathScore) { return s2.mathScore - s1.mathScore; } else if (s1.englishScore!= s2.englishScore) { return s2.englishScore - s1.englishScore; } else { return s1.id - s2.id; } }); for (Student student : students) { System.out.println(student.name + " " + student.totalScore + " " + student.mathScore + " " + student.englishScore); } } } ``` 额外需要注意的逻辑点: 1. 确保学生对象的属性设置和比较逻辑的准确性。 2. 对于排序比较的条件判断,要考虑边界情况和异常值。 3. 测试不同的学生数据组合,以验证排序结果的正确性。 ######[AI写代码神器 | 481点数解答 | 2024-11-26 10:24:07]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]