Just to show off, in Java version 12 (preview enabled), using the new Switch Expressions:
String grade = switch(avg/10) {
case 9,10 -> "A";
case 8 -> "B";
case 7 -> "C";
case 6 -> "D";
case 5 -> "E";
default -> "F";
};
or, very flexible, if not being to lazy:
String grade = switch(avg) {
case 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100 -> "A";
case 80, 81, 82, 83, 84, 85, 86, 87, 88, 89 -> "B";
// you got the idea, I AM lazy
Real (?) solution: use NavigableMap
(example TreeMap
) with its floorEntry()
or lowerEntry()
methods:
NavigableMap<Integer, String> grades = new TreeMap<>();
grades.put(90, "A");
grades.put(80, "B");
...
// usage
String grade = grades.floorEntry(avg).getValue();
values in map must eventually be adjusted