还是排序的问题,慢慢来,不懂就问
我按照第一次开的话题一个大佬给的思路写的
写完发现还是迷惑 getAvgChinese()方法获取的语文成绩最后还是没有与Banji类的对象放到一个List里面,最后测试什么也没有。。。。。。
以下是大佬的思路,还有我的代码:
第一步:班级的类里面创建一个getAvgChinese()方法,只要能得出班级学生语文成绩平均分就行,不需要另外定义属性。
public int getAvgChinese(){} // 获取班级语文平均
第二步:创建班级按语文成绩排序规则的类,这个你已经实现了。
public class ChineseScore implements Comparator<Banji>{} // 按照班级语文平均分排序
第三步:在学校的类下,创建一个geiBanJiList(){}的方法,得到所有班级对象的列表(除"主学生列表"外)。
public List<BanJi> geiBanJiList(){}
第四步:排序
List<BanJi> banJiList = school.geiBanJiList();
banJiList.sort(new SortChinese());
我的代码:
***********************************************************************************************************
package com.imooc.studentmanagement;
/**
* Student类:
*属性: 学号(stuNum)、姓名(stuName) 这两个属性为String类型
*
*数学成绩(math)、语文成绩(chinese) 这两个属性为float类型
*
*方法: 构造方法
*setter和getter方法
*hashCode()和equals()方法
*toString()方法
* @author MA BIN RAN(Vincent)
*
* 2020年6月29日-下午8:59:14
*/
public class Student {
private String stuNum;//学号
private String stuName;//姓名
private float math;//数学成绩(math)
private float chinese;//语文成绩(chinese)
//无参构造方法
public Student() {
}
//带参构造方法,并对学生的学号和姓名属性赋值
public Student(String stuNum,String stuName) {
this.setStuNum(stuNum);
this.setStuName(stuName);
}
//getter setter方法
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;
}
//HashCode equals
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((stuName == null) ? 0 : stuName.hashCode());
result = prime * result + ((stuNum == null) ? 0 : stuNum.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
//判断对象是否相等,相等直接返回true
if(this==obj) {
return true;
}
//this!=obj
if(obj.getClass()==Student.class) {
Student student = (Student)obj;
return (stuNum.equals(student.getstuNum()))&&
(stuName.equals(student.getStuName()));
}
return false;
}
//重写toString方法
@Override
public String toString() {
return "学生信息为: 学号: "+this.getstuNum()+",姓名: "+this.getStuName();
}
}
***********************************************************************************************************
package com.imooc.studentmanagement;
import java.util.ArrayList;
import java.util.List;
/**
* Banji类
*
* @author MA BIN RAN(Vincent)
*
* 2020年6月30日-上午11:54:14
*/
public class Banji {
private String classId;// 班级编号(classId)
private String className;// 班级名称(className)
private List<Student> stuList;// 存放学生的集合(stuList)
// 无参构造方法
public Banji() {
}
// 带参构造方法
public Banji(String classId, String className) {
this.setClassId(classId);
this.setClassName(className);
stuList = new ArrayList<Student>();
}
// setter getter方法
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 void setStuList(List<Student> stuList) {
this.stuList = stuList;
}
public List<Student> getStuList() {
return stuList;
}
/**
* 将学生添加到班级
*
* @param stu 要添加的学生
*/
public void addStudent(Student stu) {
// 检查是否重复
boolean flag = false;// 判断学生列表中的学生是否存在
for (Student stu1 : stuList) {
if (stu1.equals(stu)) {// equals判断内容是否相同的
flag = true;
break;
}
}
if (flag) {
System.out.println("该学生已存在列表中!添加失败!");
} else {
stuList.add(stu);
}
}
/**
* 显示班级里所有学生的信息
*/
public void displayAllStudent() {
System.out.println("该班级所有学生信息为: ");
for (Student stu : stuList) {
System.out.println(stu);
}
}
/**
* 通过学号查找学生
*
* @param stuNum 学号
* @return 要找的学生
*/
public Student searchStudentByNum(String stuNum) {
Student stu = null;
for (Student stu1 : stuList) {
if (stu1.getstuNum().equals(stuNum)) {
stu = stu1;
break;
}
}
return stu;
}
List<Float> chineseScoreList=new ArrayList<Float>();
/**
* 输入班级学生的语文成绩
*
* @param stuNum 要输入语文成绩的学生
* @param score 要输入的语文成绩 思路: 通过对用户输入的学生学号遍历存放学生的集合,找出与用户输入学号一致的学生
* 然后利用遍历出来的学生使用相关的setter方法插入成绩
*/
public void insertChineseScore(String stuNum, float score) {
// 根据stuNum查询到相关的学生,然后进行修改
Student stu = searchStudentByNum(stuNum);
if (stu == null) {
System.out.println("没有找到学号为" + stuNum + "对应的学生");
} else {
stu.setChinese(score);
chineseScoreList.add(score);
System.out.print(stu);
System.out.println(",语文成绩: " + stu.getChinese());
}
}
/**
* 获取班级平均成绩
* @return 班级平均成绩
*/
public float getAvgChinese() {
float avgChinese=0,totChinese=0;
for(int i=0;i<chineseScoreList.size();i++) {
totChinese=totChinese+chineseScoreList.get(i);
}
avgChinese=totChinese/chineseScoreList.size();
return avgChinese;
}
/**
* 输入班级学生的数学成绩
*
* @param stuNum 要输入数学成绩的学生
* @param score 要输入的数学成绩 思路: 通过对用户输入的学生学号遍历存放学生的集合,找出与用户输入学号一致的学生
* 然后利用遍历出来的学生使用相关的setter方法插入成绩
*/
public void insertMathScore(String stuNum, float score) {
// 根据stuNum查询到相关的歌曲,然后进行修改
Student stu = searchStudentByNum(stuNum);
if (stu == null) {
System.out.println("没有找到学号为" + stuNum + "对应的学生");
} else {
stu.setMath(score);
System.out.print(stu);
System.out.println(",数学成绩: " + stu.getMath());
}
}
/**
* 删除学生信息
*
* @param stuNum 要删除学生的学号
*/
public void deleteStudent(String stuNum) {
Student stu = searchStudentByNum(stuNum);
if (stu == null) {
System.out.println("删除失败!\n没有找到学号为" + stuNum + "的学生");
} else {
stuList.remove(stu);
System.out.println("学号为" + stuNum + "的学生删除成功!");
}
}
//重写 toString方法
@Override
public String toString() {
return "班级编号: "+this.getClassId()+",班级名称: "+this.getClassName();
}
}
***********************************************************************************************************
package com.imooc.studentmanagement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
*
* @author MA BIN RAN(Vincent)
*
* 2020年7月1日-下午2:45:11
*/
public class School {
Map<String,Banji> schoolMap;//存放班级列表集合
//构造方法
public School() {
schoolMap=new HashMap<String,Banji>();
}
//getter setter 方法
public void setShoolMap(Map<String,Banji> schoolMap) {
this.schoolMap=schoolMap;
}
public Map<String,Banji> getShoolMap(){
return schoolMap;
}
/**
* 添加班级
* @param banji 要添加的班级
*/
public void addBanji(Banji banji) {
//班级名称作为key值
schoolMap.put(banji.getClassName(), banji);
}
/**
* 删除班级
* @param banji 要删除的班级
*/
public void deleteBanji(Banji banji) {
schoolMap.remove(banji.getClassName());
System.out.println("删除成功!");
}
/**
* 通过班级名称查询班级
* @param className 要查询的班级名称
* @return 查询到的班级
*/
public Banji searchBanJiByName(String className) {
Banji banji=null;
Set<String> banJiSet=schoolMap.keySet();
for(String s:banJiSet) {
if(s.equals(className)) {
banji=schoolMap.get(s);
break;
}
}
return banji;
}
/**
* 展示所有的班级
*/
public void displayBanJiName() {
System.out.println("所有班级名称为: ");
Set<String> banJiSet=schoolMap.keySet();
for(String s: banJiSet) {
System.out.println(s);
}
}
public List<Banji> getBanJiList(){
List<Banji> banJiList = new ArrayList<>();
for(Banji banji: schoolMap.values()) {
if(banji.getClassName().equals("主学生列表")) {
continue;
}else {
banJiList.add(banji);
}
}
return banJiList;
}
}
***********************************************************************************************************
package com.imooc.studentmanagement;
import java.util.Comparator;
public class ChineseScore implements Comparator<Banji> {
@Override
public int compare(Banji arg0, Banji arg1) {
//按成绩升序排序
int f1=(int)(arg0.getAvgChinese());
int f2=(int)(arg1.getAvgChinese());
return f1-f2;
}
}
***********************************************************************************************************
package com.imooc.studentmanagement;
import java.util.Collections;
import java.util.List;
public class Test1 {
//Student类测试
public void studentTest() {
Student stu1=new Student("s001","小明");
Student stu2=new Student("s002","小王");
Student stu3=new Student("s003","小张");
Student stu4=new Student("s003","小张");
System.out.println(stu1);
System.out.println(stu2);
System.out.println(stu3);
System.out.println("stu3==stu4?"+stu3.equals(stu4));
System.out.println("stu1==stu2?"+stu1.equals(stu2));
}
//班级类测试
public void banJiTest() {
Student stu1=new Student("s001","小明");
Student stu2=new Student("s002","小王");
Student stu3=new Student("s003","小张");
Student stu4=new Student("s004","小吴");
Banji class1=new Banji("1","一班");
class1.addStudent(stu1);
class1.addStudent(stu2);
class1.addStudent(stu3);
class1.addStudent(stu4);
class1.displayAllStudent();
//测试通过学号找学生
Student stu5=class1.searchStudentByNum("s001");
System.out.println(stu5);
//测试输入班级学生的语文成绩
class1.insertChineseScore("s001", 70f);
class1.insertChineseScore("s002", 60f);
class1.insertChineseScore("s003", 50f);
//测试通过学生学号删除学生
class1.deleteStudent("s004");
class1.displayAllStudent();
class1.getAvgChinese();
//测试语文平均成绩
System.out.println("语文平均成绩为: ");
System.out.println(class1.getAvgChinese());
}
//测试School类
public void schoolTest() {
Student stu1=new Student("s001","小明");
Student stu2=new Student("s002","小王");
Student stu3=new Student("s003","小张");
Student stu4=new Student("s003","小张");
Banji mainBanJi=new Banji("0","主学生列表");
Banji class1=new Banji("1","一班");
Banji class2=new Banji("2","二班");
class1.addStudent(stu1);
class1.addStudent(stu2);
class1.addStudent(stu3);
class1.displayAllStudent();
School school=new School();
//测试添加班级
school.addBanji(class1);
school.addBanji(class2);
//展示所有的班级
System.out.println("删除前: ");
school.displayBanJiName();
//删除指定班级
school.deleteBanji(class2);
System.out.println("删除后: ");
school.displayBanJiName();
//通过班级名称查询班级
Banji banji=school.searchBanJiByName("一班");
System.out.println(banji);
//对各班语文成绩按平均分进行由大到小排序
List<Banji> banJiList = school.getBanJiList();
Collections.sort(banJiList, new ChineseScore());
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Test1 t1=new Test1();
//t1.studentTest();
//t1.banJiTest();
t1.schoolTest();
}
}
正在回答
//对各班语文成绩按平均分进行由大到小排序
List<Banji> banJiList = school.getBanJiList();
Collections.sort(banJiList, new ChineseScore());
大哥,你光排序,不输出,那不是什么都没有?
这个执行完banJiList已经排序好了,用for循环输出就好了
同学你好,老师测试贴出内容,实现了排序,例如:
package com.imooc.studentmanagement; import java.util.Collections; import java.util.List; public class Test1 { //Student类测试 public void studentTest() { Student stu1=new Student("s001","小明"); Student stu2=new Student("s002","小王"); Student stu3=new Student("s003","小张"); Student stu4=new Student("s003","小张"); System.out.println(stu1); System.out.println(stu2); System.out.println(stu3); System.out.println("stu3==stu4?"+stu3.equals(stu4)); System.out.println("stu1==stu2?"+stu1.equals(stu2)); } //班级类测试 public void banJiTest() { Student stu1=new Student("s001","小明"); Student stu2=new Student("s002","小王"); Student stu3=new Student("s003","小张"); Student stu4=new Student("s004","小吴"); Banji class1=new Banji("1","一班"); class1.addStudent(stu1); class1.addStudent(stu2); class1.addStudent(stu3); class1.addStudent(stu4); class1.displayAllStudent(); //测试通过学号找学生 Student stu5=class1.searchStudentByNum("s001"); System.out.println(stu5); //测试输入班级学生的语文成绩 class1.insertChineseScore("s001", 70f); class1.insertChineseScore("s002", 60f); class1.insertChineseScore("s003", 50f); //测试通过学生学号删除学生 class1.deleteStudent("s004"); class1.displayAllStudent(); class1.getAvgChinese(); //测试语文平均成绩 System.out.println("语文平均成绩为: "); System.out.println(class1.getAvgChinese()); } //测试School类 public void schoolTest() { Student stu1=new Student("s001","小明"); Student stu2=new Student("s002","小王"); Student stu3=new Student("s003","小张"); Student stu4=new Student("s004","小张"); Banji mainBanJi=new Banji("0","主学生列表"); Banji class1=new Banji("1","一班"); Banji class2=new Banji("2","二班"); class1.addStudent(stu1); class1.addStudent(stu2); class1.addStudent(stu3); class2.addStudent(stu4); class1.insertChineseScore("s001", 70f); class1.insertChineseScore("s002", 60f); class1.insertChineseScore("s003", 50f); class2.insertChineseScore("s004", 65f); class1.addStudent(stu1); class1.addStudent(stu2); class1.addStudent(stu3); class2.addStudent(stu4); class1.displayAllStudent(); School school=new School(); //测试添加班级 school.addBanji(class1); school.addBanji(class2); //对各班语文成绩按平均分进行由大到小排序 List<Banji> banJiList = school.getBanJiList(); Collections.sort(banJiList, new ChineseScore()); for (Banji banji2 : banJiList) { System.out.println(banji2.getClassName()+"的平均分是"+banji2.getAvgChinese()); //System.out.println(banji2); } } public static void main(String[] args) { // TODO Auto-generated method stub Test1 t1=new Test1(); //t1.studentTest(); //t1.banJiTest(); t1.schoolTest(); } }
对应的执行结果:
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星