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;
}
![](https://profile-avatar.csdnimg.cn/f38512821cc54f9fab37094218ae319e_qq_63759517.jpg!4)
java中用switch和case语句怎么代替if语句
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- wyz0923 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 网络科学导论,网络控制
- ¥100 安卓tv程序连接SQLSERVER2008问题
- ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
- ¥15 metadata提取的PDF元数据,如何转换为一个Excel
- ¥15 关于arduino编程toCharArray()函数的使用
- ¥100 vc++混合CEF采用CLR方式编译报错
- ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
- ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
- ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
- ¥50 如何openEuler 22.03上安装配置drbd