Using a key to map to a range of values. Letter Grade to exam score example [duplicate]

时间秒杀一切 提交于 2020-04-07 02:21:08

问题


As the title suggests. I need the functionality of where the grade "A" is returned for any score between 90-100. Or in this case, whatever cutoff the user decides to set based on a curve or something. This is what I had but I can't think of a way to have it do what I want it to.

private HashMap<String, Double> letterGrade = new HashMap<String, Double>();

public void setgradeCutOff(String[] letterGrades, double[] cutoffs) {
        for(int i = 0; i < letterGrades.length; i++){
            letterGrade.put(letterGrades[i], cutoffs[i]);
        }

    }
public String getGrade(String studentName) {
    //Returns a student's average score for all exams
    double averageScore = getAverageExamGrade(studentName);
    for(Entry<String, Double> entry : letterGrade.entrySet()){
        if(entry.getValue() == averageScore){
            return entry.getKey();
        }
    }
    return null;
}

This only works if the averageScore is exactly one of the cutoff values. How can I modify it so that the string key maps to a range as specified by the setGradeCutOff method?

This is how I expect it to work:

setLetterGradesCutoffs(new String[]{"A","B","C","D","F"}, 
                   new double[] {85,70,60,50,0});

Laura. Average exam score: 87 Grade: A
Peter. Average exam score: 72 Grade: B
Miranda. Average exam score: 67 Grade: C

回答1:


No need for a third-party library, TreeMap can do it, using the floorEntry() method (Java 6+):

private static TreeMap<Double, String> gradeMap = new TreeMap<>();
static {
    gradeMap.put(85.0, "A");
    gradeMap.put(70.0, "B");
    gradeMap.put(60.0, "C");
    gradeMap.put(50.0, "D");
    gradeMap.put( 0.0, "F");
}
private static void printGrade(double score) {
    System.out.printf("Average exam score: %s Grade: %s%n",
                      NumberFormat.getInstance().format(score),
                      gradeMap.floorEntry(score).getValue());
}

Test

public static void main(String[] args) {
    printGrade(87);
    printGrade(72);
    printGrade(67);
    printGrade(69.99);
    printGrade(70.00);
    printGrade(70.01);
}

Output

Average exam score: 87 Grade: A
Average exam score: 72 Grade: B
Average exam score: 67 Grade: C
Average exam score: 69.99 Grade: C
Average exam score: 70 Grade: B
Average exam score: 70.01 Grade: B



回答2:


You can use Guava's RangeMap to map the range of grades to a letter:

RangeMap<Integer, String> gradeLetter = ImmutableRangeMap.builder()
    .put(Range.closed(90, 100), "A")
    .put(Range.closed(60, 89), "B")
    // ...
    .build();

public String getGrade(String studentName) {
    int averageScore = getAverageExamGrade(studentName);
    return gradeLetter.get(averageScore);
}


来源:https://stackoverflow.com/questions/33620530/using-a-key-to-map-to-a-range-of-values-letter-grade-to-exam-score-example

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!