public static Grade scoreLevel(int score) {
if (score >= 90 && score <= 100) {
System.out.println("Excellent");
return Grade.A;
} else if (score >= 80 && score < 90) {
System.out.println("Good");
return Grade.B;
} else if (score >= 60 && score < 80) {
System.out.println("Average");
return Grade.C;
} else if (score >= 0 && score < 60) {
System.out.println("Bad");
return Grade.D;
}
return null;
}
java中用switch和case语句怎么代替if语句
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- heling_m6ss 2022-03-21 21:06关注
将分数控制在十位以内,这样就连可以列举完,然后利用case 穿透来满足范围查询。建议不要这样做,平级if else 只会执行一个判断。 可以使用模板加策略来优化
score = score/10; switch(score){ case 10: case 9: System.out.println("Excellent"); return Grade.A; case 8: System.out.println("Good"); return Grade.B; case 7: case 6: System.out.println("Average"); return Grade.C; case 5: case 4: case 3: case 2: case 1: case 0: System.out.println("Average"); return Grade.C; default: return null;
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
- ¥15 求daily translation(DT)偏差订正方法的代码
- ¥15 js调用html页面需要隐藏某个按钮
- ¥15 ads仿真结果在圆图上是怎么读数的
- ¥20 Cotex M3的调试和程序执行方式是什么样的?
- ¥20 java项目连接sqlserver时报ssl相关错误
- ¥15 一道python难题3
- ¥15 牛顿斯科特系数表表示
- ¥15 arduino 步进电机
- ¥20 程序进入HardFault_Handler