sortMathByAverage() 的测试出现问题

sortMathByAverage() 的测试出现问题

相关代码(School类):

package com.comp.stumanage;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class School {
// 属性:存放班级的集合
private Map<String, BanJi> schoolMap;

// 无参构造
public School() {
// 注意:实例化 schoolMap
schoolMap = new HashMap<String, BanJi>();
}

// getters and setters
public Map<String, BanJi> getSchoolMap() {
return schoolMap;
}

public void setSchoolMap(Map<String, BanJi> schoolMap) {
this.schoolMap = schoolMap;
}

/**
* 显示所有班级名称
*/
public void displayBanJiName() {
// 注意:Map能生成的 set只有 .keySet() 和 .entrySet(),没有 .valueSet()
Set<String> nameset = schoolMap.keySet();
for (String s : nameset) {
System.out.println(s);
}
}

/**
* 添加班级
*
* @param bj 班级信息
*/
public void addBanji(BanJi bj) {
Set<String> keyset = schoolMap.keySet();
String bjName = bj.getClassName();
for (String s : keyset) {
if (s.equals(bjName)) {
System.out.println("班级已存在,请重新输入...");
return;
}
}
schoolMap.put(bjName, bj);
System.out.println("添加成功");
}

/**
* 根据班级名字找到班级
*
* @param className 要找到的班级的名字
* @return 班级
*/
public BanJi searchByName(String className) {
Set<String> keyset = schoolMap.keySet();
// 注意:因为在没有找到对应班级名时应该返回 null,所以就直接设定返回变量为 null就好了
BanJi bj = null;
for (String s : keyset) {
if (s.equals(className)) {
bj = schoolMap.get(s);
break;
}
}
return bj;
}

/**
* 删除班级
*
* @param bj 要删除的班级
*/
public void deleteBanji(BanJi bj) {
Set<String> keyset = schoolMap.keySet();
for (String s : keyset) {
if (schoolMap.get(s).equals(bj)) {
schoolMap.remove(s);
return;
}
}

}

/**
* 对各班的语文平均成绩进行排序
*/
public void sortChineseByAverage() {
Iterator<Entry<String, BanJi>> it = schoolMap.entrySet().iterator();
Map<Double, String> averageScoreMap = new HashMap<Double, String>();
List<Double> sortList = new ArrayList<Double>();
// 做 map,key 为平均成绩,value 为班级名,然后对平均成绩进行排序,之后就可以通过 getValue 获得班级名
while (it.hasNext()) {
BanJi bj = it.next().getValue();
List<Student> stuList = bj.getStuList();
double sum = 0;
for (Student stu : stuList) {
sum += stu.getChinese();
}
sum = sum / bj.getStuList().size();
averageScoreMap.put(sum, bj.getClassName());
sortList.add(sum);
}
Collections.sort(sortList, new ScoreComparator());
for (Double d : sortList) {
String s = averageScoreMap.get(d);
System.out.println("班级:" + s + ",中文平均成绩为:" + d);
}

}

/**
* 对各班的数学平均成绩进行排序
*/
public void sortMathByAverage() {
Iterator<Entry<String, BanJi>> it = schoolMap.entrySet().iterator();
Map<Double, String> averageScoreMap = new HashMap<Double, String>();
List<Double> sortList = new ArrayList<Double>();
// 做 map,key 为平均成绩,value 为班级名,然后对平均成绩进行排序,之后就可以通过 getValue 获得班级名
while (it.hasNext()) {
BanJi bj = it.next().getValue();
List<Student> stuList = bj.getStuList();
double sum = 0;
for (Student stu : stuList) {
sum += stu.getMath();
}
sum = sum / bj.getStuList().size();
averageScoreMap.put(sum, bj.getClassName());
sortList.add(sum);
}
Collections.sort(sortList,new ScoreComparator());
for (Double d : sortList) {
String s = averageScoreMap.get(d);
System.out.println("班级:" + s + ",数学平均成绩为:" + d);
}

}
}


相关代码(Comparator 实现):

package com.comp.stumanage;

import java.util.Comparator;

public class ScoreComparator implements Comparator<Double> {

@Override
public int compare(Double arg0, Double arg1) {
// TODO Auto-generated method stub
int n=new Double(arg1-arg0).intValue();
return n;
}


}


相关代码(Test类):

package com.comp.test;

import com.comp.stumanage.BanJi;
import com.comp.stumanage.School;
import com.comp.stumanage.Student;

public class StuManageTest {

public void stuTest() {
...
}

public void testBanJi() {
...
}

public void testSchool() {
//测试构造函数
School school=new School();
BanJi bj=new BanJi("b001","一班");
BanJi bj0=new BanJi("b001","一班");

//测试 addBanji
school.addBanji(bj);
school.addBanji(bj0);

//测试 displayBanJiName
System.out.println("***");
school.displayBanJiName();

//测试 searchByName
System.out.println("***");
if((bj=school.searchByName("零班"))==null)
System.out.println("该班级不存在");
else
System.out.println(bj.getClassName());

if((bj=school.searchByName("一班"))==null)
System.out.println("该班级不存在");
else
System.out.println(bj.getClassName());

//测试 deleteBanji
System.out.println("***");
BanJi bj2=new BanJi("b002","二班");
school.addBanji(bj2);
school.deleteBanji(bj);
school.displayBanJiName();

//测试 sortChineseByAverage
System.out.println("***");
BanJi bj3=new BanJi("b003","三班");
school.addBanji(bj);
school.addBanji(bj3);
Student stu1=new Student("s001","张三",3,86);
Student stu2=new Student("s002","李四",2,93);
Student stu3=new Student("s003","王五",3,90);
Student stu11=new Student("s0011","张三",50,87);
Student stu22=new Student("s0022","李四",50,94);
Student stu33=new Student("s0033","王五",50,91);
bj.addStudent(stu1);
bj2.addStudent(stu2);
bj3.addStudent(stu3);
bj.addStudent(stu11);
bj2.addStudent(stu22);
bj3.addStudent(stu33);
school.sortChineseByAverage();

//测试 sortMathByAverage
System.out.println("***");
school.sortMathByAverage();
}

public static void main(String[] args) {
StuManageTest test=new StuManageTest();
test.testSchool();
}
}


相关截图:

http://img1.sycdn.imooc.com//climg/60c300610972020905900324.jpg


问题描述:

为什么在 sortMathByAverage() 中平均成绩排序没有从大到小排呢?

sortMathByAverage() 的代码和 sortChineseByAverage() 是一样的。

是 Comparator() 哪里写错了吗?

正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,查看代码数学成绩排序和语文成绩排序大体一致,由于同学没有反馈完整代码老师无法进行测试,同学可以在数学成绩排序的位置打断点运行试试,看看是哪里有问题,如果没有找到,建议同学将剩余代码进行反馈,方便老师进行测试

祝学习愉快~

  • 慕神74214486 提问者 #1

    哦哦,我看到问题在哪里了。是因为在 Comparator 里面两个 double 值相差为 0.5,进行强制类型转换的时候就变成 0 了,comparator 就判定两个值相等。


    我都没有想起来还有打断点的功能。谢谢。

    2021-06-11 17:49:01
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师