Java实现筛选全成绩高于阈值的学生名单问题求助
问题:筛选所有成绩严格高于指定阈值的学生名单
需要实现逻辑:返回所有成绩均严格高于特定阈值的学生姓名列表,尝试嵌套if和while循环后仍无法正确实现,现有代码要么错误包含低于阈值的学生,要么把所有学生都加入列表。
原实现代码
import java.util.LinkedList; import java.util.List; import java.util.Map; public class ExampleMap { /** * Return a list of "high scoring" students --- High scoring students are * students who have all grades strictly greater than the given threshold. * * @param scoresByApplicantName A map of applicant names to applicant scores * @param scoreThreshold The score threshold * @return The list of high-scoring applicant names */ public static List<String> highScoringStudents(Map<String, List<CourseGrade>> scoresByApplicantName, int scoreThreshold) { List<String> highScoringStudents = new LinkedList<>(); /* * Build a list of the names of applicants who have scores strictly greater than * the given threshold. */ for (Map.Entry<String, List<CourseGrade>> current : scoresByApplicantName.entrySet()) { List<CourseGrade> course_grades = current.getValue(); for (CourseGrade courses : course_grades) { if (courses.getScore() > scoreThreshold) { highScoringStudents.add(current.getKey()); } } } return highScoringStudents; } }
CourseGrade类代码(无需修改)
/* * This file should remain unchanged. */ public class CourseGrade { private String courseName; private int score; /** * This is a constructor. It creates a new CourseGrade with the specified scores. * @param courseName * @param score */ public CourseGrade(String courseName, int score) { this.courseName = courseName; this.score = score; } public String getCourseName() { return this.courseName; } public int getScore() { return this.score; } }
测试用例
@Test public void testExampleMap1() { Map<String, List<CourseGrade>> courseListsByStudent = new HashMap<>(); courseListsByStudent.put("Julie", Arrays.asList( new CourseGrade("CPE 123", 89), new CourseGrade("CPE 101", 90), new CourseGrade("CPE 202", 99), new CourseGrade("CPE 203", 100), new CourseGrade("CPE 225", 89))); courseListsByStudent.put("Paul", Arrays.asList( new CourseGrade("CPE 101", 86), new CourseGrade("CPE 202", 80), new CourseGrade("CPE 203", 76), new CourseGrade("CPE 225", 80))); courseListsByStudent.put("Zoe", Arrays.asList( new CourseGrade("CPE 123", 99), new CourseGrade("CPE 203", 91), new CourseGrade("CPE 471", 86), new CourseGrade("CPE 473", 90), new CourseGrade("CPE 476", 99), new CourseGrade("CPE 572", 100))); List<String> expected = Arrays.asList("Julie", "Zoe"); /* * Why compare HashSets here? Just so that the order of the * elements in the list is not important for this test. */ assertEquals(new HashSet<>(expected), new HashSet<>(ExampleMap.highScoringStudents( courseListsByStudent, 85))); }
错误原因分析
原代码逻辑完全偏离需求:只要学生有一门课程成绩高于阈值,就会把该学生姓名重复加入结果列表(有几门达标就加几次),但实际需要的是所有课程成绩都严格高于阈值的学生才符合条件。
修正后的代码
import java.util.LinkedList; import java.util.List; import java.util.Map; public class ExampleMap { /** * Return a list of "high scoring" students --- High scoring students are * students who have all grades strictly greater than the given threshold. * * @param scoresByApplicantName A map of applicant names to applicant scores * @param scoreThreshold The score threshold * @return The list of high-scoring applicant names */ public static List<String> highScoringStudents(Map<String, List<CourseGrade>> scoresByApplicantName, int scoreThreshold) { List<String> highScoringStudents = new LinkedList<>(); for (Map.Entry<String, List<CourseGrade>> current : scoresByApplicantName.entrySet()) { List<CourseGrade> courseGrades = current.getValue(); // 先默认该学生所有成绩达标 boolean allScoresAboveThreshold = true; for (CourseGrade courseGrade : courseGrades) { // 只要有一门成绩不达标,立即标记为不符合并终止循环 if (courseGrade.getScore() <= scoreThreshold) { allScoresAboveThreshold = false; break; } } // 确认所有成绩都达标后,再加入结果列表 if (allScoresAboveThreshold) { highScoringStudents.add(current.getKey()); } } return highScoringStudents; } }
修正逻辑说明
- 对每个学生初始化标记位,默认其所有成绩达标
- 遍历学生的课程成绩,一旦发现某门成绩≤阈值,立即修改标记位并跳出循环(减少不必要的遍历)
- 遍历结束后,仅当标记位仍为
true时,将学生姓名加入结果列表
该逻辑完全匹配需求,同时避免了重复添加的问题,能通过给定的测试用例。
内容的提问来源于stack exchange,提问作者awoobis
相关产品推荐
相关产品推荐

