关于排序的compareTo方法问题
为啥上面讲的没错误 下面我自己写的就不行呢
再问一下 这个compareTo方法的返回类型 参数 及 compareTo方法名可以改吗
//完整代码 package com.mooc.StudentFile; import java.util.ArrayList; import java.util.List; public class BanJi implements Comparable<BanJi>{ private String classId; private String className; List<Student> stuList=new ArrayList<Student>(); public BanJi() { } public BanJi(String classId,String className) { setClassId(classId); setClassName(className); stuList=new ArrayList<Student>(); } @Override public String toString() { return "班级信息:\n班级编号:" + classId + ", 班级名:" + className+"\n"; } /** * 添加学生 * @param stu 要添加的学生 */ public void addStudent(Student stu) { boolean flag=true; for(Student stu1:stuList) { if(stu1.equals(stu)) { flag=false;break; } } if(flag==true) {stuList.add(stu); } else System.out.println("添加失败,该学生已存在 "); } /** * 按学号查找学生 * @param stuNum * @return */ public Student searchNum(String stuNum) { for(Student stu:stuList) { if(stu.getStuNum().equals(stuNum)) { System.out.println("找到了"+stu); return stu; } } System.out.println("没找到"); return null; } /** * 添加学生语文成绩 * @param stuNum 学号 * @param score 语文成绩 */ public void addChinese(String stuNum,float score) { boolean flag=false; for(Student stu:stuList) { if(stu.getStuNum().equals(stuNum)) { stu.setChinese(score);flag=true; break; } } if(flag=false)System.out.println("学号错误 没有此学生"); } /** * 删除学生 * @param stuNum 学号 */ public void delete(String stuNum){ boolean flag=false; for(Student stu:stuList) { if(stu.getStuNum().equals(stuNum)) {System.out.println("删除成功\n学生"+stu.getStuName()+"已被删除"); stuList.remove(stu); flag=true;break;} } if(flag==false)System.out.println("删除失败 没有这个学生"); } /** * 添加学生数学成绩 * @param stuNum 学号 * @param score 数学成绩 */ public void addMath(String stuNum,float score) { boolean flag=false; for(Student stu:stuList) { if(stu.getStuNum().equals(stuNum)) { stu.setMath(score);flag=true; break; } } if(flag=false)System.out.println("学号错误 没有此学生"); } /** * 展示所有学生 */ public void show() { for(Student stu1:stuList) { System.out.println(stu1); } } public String getClassId() { return classId; } public void setClassId(String classId) { this.classId = classId; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public double AveChinese() { double average=0.0; for(Student stu:stuList) { average=(average+stu.getChinese())/stuList.size(); } return average; } public double AveMath() { double average=0.0; for(Student stu:stuList) { average=(average+stu.getMath())/stuList.size(); } return average; } @Override public int compareTo(BanJi o) { // TODO Auto-generated method stub double mean1=this.AveChinese(); double mean2=o.AveChinese();} int n=new Double(mean2-mean1).intValue(); return n; } }
8
收起
正在回答
2回答
同学你好,代码中多了一个大括号,将其删除后再测试运行试下。
祝学习愉快~
起風了3592092
2022-03-21 10:58:17
//学生类 package com.mooc.StudentFile; import java.util.Objects; public class Student { private String stuNum; private String stuName; private float math; private float chinese; public Student() { } public Student(String stuNum,String stuName,float math,float chinese) { setStuNum(stuNum); setStuName(stuName); setMath(math); setChinese(chinese); } @Override public int hashCode() { return Objects.hash(chinese, math, stuName, stuNum); } @Override public boolean equals(Object obj) { if(this==obj) { return true; } if(obj.getClass()==Student.class) { Student student=(Student)obj; return student.getStuNum().equals(stuNum)&&(student.getStuName()==stuName)&& (student.getMath()==math)&&(student.getChinese()==chinese); } return false; } @Override public String toString() { return "学生信息为:\n学号:" + stuNum + " 姓名:" + stuName + " 数学成绩:" + math + " 语文成绩:" + chinese ; } public String getStuNum() { return stuNum; } public void setStuNum(String stuNum) { this.stuNum = stuNum; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public float getMath() { return math; } public void setMath(float math) { this.math = math; } public float getChinese() { return chinese; } public void setChinese(float chinese) { this.chinese = chinese; } }
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星