对学生成绩进行排序。学生信息包括:姓名name,学号id,语文成绩,数学成绩,英语成绩。 排序方式: 首选总分从高到低排序; 若总分相同,按语文成绩从高到低排序; 若总分相同且语文成绩相同,按数学成绩从高到低排序; 若总分相同且语文、数学成绩相同,按英语成绩从高到低排序; 若总分相同,且各科成绩相同,按学号从小到大排序。 提示:在eclipse中的控制台console,可使用ctrl+z强制结束数据输入。 输入格式: 每一行为一个学生的信息:姓名,学号,语文成绩,数学成绩,英语成绩 输出格式: 输出排序完后的学生系信息:姓名,学号,总成绩,语文成绩,数学成绩,英语成绩。 输入样例: 在这里给出一组输入。例如: 张三 20201003 80 82 89 李四 20201002 70 81 49 张三 20201001 50 84 90 输出样例: 在这里给出相应的输出。例如: 张三, 20201003, 251, 80, 82, 89 张三, 20201001, 224, 50, 84, 90 李四, 20201002, 200, 70, 81, 49
给你写,要收点费用的。因为耗时间
你这代码量有点多,需要。
package com.sxt.pojo; import java.util.Objects; /** * 学生信息 * * @author 刘逸晖 */ public class StudentScore { /** * 学生的学号 */ private int id = 0; /** * 学生的姓名 */ private String name = ""; /** * 学生的语文成绩 */ private int chineseScore = 0; /** * 学生的数学成绩 */ private int mathScore = 0; /** * 学生的英语成绩 */ private int englishScore = 0; /** * 学生个科的总成绩 */ private int totalScore = 0; /** * 比大小 * 这里题目有点问题,语文成绩和数学成绩相同的话,如果英语成绩不同,那总成绩就不同,根本不用判断英语成绩 * * @param studentScore 预比较的对象 * @return 若自身比传入的对象大则返回true,否则返回false */ public boolean compareTo(StudentScore studentScore) { if (this.totalScore > studentScore.getTotalScore()) { return true; } else if (this.totalScore < studentScore.getTotalScore()) { return false; } else {//两者总分相等,比较语文成绩 if (this.chineseScore > studentScore.getChineseScore()) { return true; } else if (this.chineseScore < studentScore.getChineseScore()) { return false; } else {//两者总分和语文成绩军相同,比较数学成绩 if (this.mathScore > studentScore.getMathScore()) { return true; } else if (this.mathScore < studentScore.getMathScore()) { return false; } else {//两者总分和语文、数学成绩军相等,比较英语成绩 if (this.englishScore > studentScore.getEnglishScore()) { return true; } else if (this.englishScore < studentScore.getEnglishScore()) { return false; } else {//两者总成绩和语文、数学、英语成绩军相等,比较学号 if (this.id < studentScore.getId()) { return true; } else if (this.id > studentScore.getId()) { return false; } else {//两者学号相同,返回true。按道理学号是不能相同的 return true; } } } } } } public StudentScore() { } public StudentScore(int id, String name, int chineseScore, int mathScore, int englishScore, int totalScore) { this.id = id; this.name = name; this.chineseScore = chineseScore; this.mathScore = mathScore; this.englishScore = englishScore; this.totalScore = totalScore; } @Override public String toString() { return "StudentScore{" + "id=" + id + ", name='" + name + '\'' + ", chineseScore=" + chineseScore + ", mathScore=" + mathScore + ", englishScore=" + englishScore + ", totalScore=" + totalScore + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; StudentScore that = (StudentScore) o; return id == that.id && chineseScore == that.chineseScore && mathScore == that.mathScore && englishScore == that.englishScore && totalScore == that.totalScore && Objects.equals(name, that.name); } @Override public int hashCode() { return Objects.hash(id, name, chineseScore, mathScore, englishScore, totalScore); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getChineseScore() { return chineseScore; } public void setChineseScore(int chineseScore) { if (this.chineseScore == 0) { this.totalScore = totalScore + chineseScore; } else { this.totalScore = totalScore + (this.chineseScore - chineseScore); } this.chineseScore = chineseScore; } public int getMathScore() { return mathScore; } public void setMathScore(int mathScore) { if (this.mathScore == 0) { this.totalScore = this.totalScore + mathScore; } else { this.totalScore = this.totalScore + (this.mathScore - mathScore); } this.mathScore = mathScore; } public int getEnglishScore() { return englishScore; } public void setEnglishScore(int englishScore) { if (this.englishScore == 0) { this.totalScore = this.totalScore + englishScore; } else { this.totalScore = this.totalScore + (this.englishScore - englishScore); } this.englishScore = englishScore; } public int getTotalScore() { return totalScore; } public void setTotalScore(int totalScore) { this.totalScore = totalScore; } }
package com.sxt.application; import com.sxt.pojo.StudentScore; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; /** * 学生系统 * 如果你要是会容器、异常这些的话到是不用这么费劲 * 异常这些我就随便处理了下,还是有些问题的,不过不影响使用 * * @author 刘逸晖 */ public class StudentApplication { private static int studentScoreCount = 0;//记录了多少个学生成绩 private static Scanner scanner = new Scanner(System.in); /** * 判断一段文本是否为空 * * @param text 需要判断的文本 * @return 为空返回true,不为空返回false */ private static boolean textIsBlank(String text) { return text == null || "".equals(text); } /** * 向控制台打印一行文本,并让用户在控制台输入一行文本 * * @param printContent 需要打印的文本 * @return 用户输入的文本 */ public static String printAndEnter(String printContent) { System.out.println(printContent); return scanner.nextLine(); } public static void main(String[] args) { StudentScore[] studentScores = new StudentScore[2];//所有输入的成绩 for (int i = 0; i < studentScores.length; i++) {//每循环一次,用户都可以输入一个学生成绩 StudentScore studentScore = new StudentScore();//当前正在输入的学生成绩 try { String enterContent = printAndEnter("如果想添加学生,请输入学生学号,最多可添加20个学生,回车键退出程序"); if (textIsBlank(enterContent)) { break; } studentScore.setId(new Integer(enterContent).intValue()); enterContent = printAndEnter("请输入学生姓名,回车键取消本次输入"); if (textIsBlank(enterContent)) { i--; continue; } studentScore.setName(enterContent); enterContent = printAndEnter("请输入学生语文成绩,回车键取消本次输入"); if (textIsBlank(enterContent)) { i--; continue; } studentScore.setChineseScore(new Integer(enterContent).intValue()); enterContent = printAndEnter("请输入学生数学成绩,回车键取消本次输入"); if (textIsBlank(enterContent)) { i--; continue; } studentScore.setMathScore(new Integer(enterContent).intValue()); enterContent = printAndEnter("请输入学生英语成绩,回车键取消本次输入"); if (textIsBlank(enterContent)) { i--; continue; } studentScore.setEnglishScore(new Integer(enterContent).intValue()); studentScores[i] = studentScore; studentScoreCount++; sort(studentScores); for (int studentScoreIndex = 0; studentScoreIndex < studentScoreCount; studentScoreIndex++) { System.out.println(studentScores[studentScoreIndex]); } } catch (NumberFormatException e) { continue; } } } /** * 对学生成绩进行排序,返回的数组仍是传入的数组本身 * 这里面用的是冒泡排序,看不懂就跳过 * * @param studentScores 需要排序的学生成绩 * @return 排序好的学生成绩 */ private static StudentScore[] sort(StudentScore[] studentScores) { if (studentScores == null || studentScoreCount < 2) { return studentScores; } /** * 循环数组中的每个成员,除最后一个外 * 每次循环都将当前成员与它的下一个成员进行比大小 * 如果当前成员比它的下一个成员大,则将当前成员与它的下一个成员交换位置,保证大的成员在后面 * 无论当前成员是否比它的下一个成员大,下次循环的时候都是拿着它的下一个成员和更后面的一个成员进行比大小,再次强调一下,下一个成员也就是最大的那一个成员 * 最后一个成员之后没有下一个成员,因此不用比较 * 经过上述操作能牌到最后的都是最大的,因此最大的那一个会在最后 * 接着我们再将这个最大的成员,也就是排在最后面的那一个成员排除,去找剩余成员中最大的那一个 * 剩余成员中最大的那一个,也就是大小仅次于最大的成员的那一个,最后它会被放在最大的那一个成员的后面 * 如此循环,不断的将最大的成员往后放,然后再找出剩余成员中最大的那一个,直到最后只剩下一个成员才停止比较 * 如果没学过冒泡排序,看不懂下面的代码就跳过吧 */ //每循环一次,就能找出来一个剩余成员中最大的成员,并将其放在最后 //因此每循环一次都可以形象的比喻成一个冒泡 for (int bubbleCount = 0; bubbleCount < studentScoreCount - 1; bubbleCount++) { //studentScoreCount-1:不便利最后一个成员 //当前循环的次数=目前有多少个泡泡 boolean sorted = true;//剩余成员是否本身就是已排好序的,只要之后数组成员在比大小的时候没有发生位置交换,就说明剩余成员已经是排好序的了 //每循环一次都比一次大小 for (int i = 0; i < studentScoreCount - bubbleCount - 1; i++) { //-bubble:不在比较排在最后面的最大数 //-1:不在比较剩余成员中的最后一个 //如果当前成员比下一个成员大,则交换位置 if (studentScores[i].compareTo(studentScores[i + 1])) { StudentScore studentScore = studentScores[i]; studentScores[i] = studentScores[i + 1]; studentScores[i + 1] = studentScore; sorted = false; } } if (sorted) {//对剩余的所有成员都便利了一遍,没有发现前面的比后面大的情况,就说明数组中的成员本身就是按顺序排列的 break; } } //现在数组中的成员是从小到大排序的,要将它变成从大到小排序的,也就是将数组倒过来,当然你在前面排序的时候也可以直接写成从大到小排列的 Collections.reverse(Arrays.asList(studentScores)); return studentScores; //真是不愿意写注释,影响我思路,不写你又不一定看得懂,让别人看到说我写你看不懂的代码,误人子弟 } }
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632