常见会有小学考试,100-90是优秀,90-80是良好,80-60是及格,60-0是较差,要求输入一个数求他的成绩等级。常规思路就是遍历。本文会尝试另一种思路。
public enum GradeEnum{
POOR(1,0,“较差”);
PASS(2,60,"及格")
GOOD(3,80,"良好");
OUTSTANDING(4,90,"优秀")
private static TreeMap<Integer,GradeEnum> treemap=new TreeMap<>();
private int index;
private int minScore;
private String grade;
static{
for(GradeEnum enum:GradeEnum.values()){
treeMap.put(minScore,enum);
}
}
public static GradeEnum getByMinScore(int score){
SoretedMap sortedMap=treemap.headMap(score+1);
if(CollectionUtils.isEmpty(sortedMap)){
return null;
}else{
return sortedMap.get(sortedMap.lastKey());
}
}
}