嵌套if以及多路if-else语句
来源:互联网 发布:淘宝装修免费一键安装 编辑:程序博客网 时间:2024/06/05 04:16
说明:
An if statement can be inside another if statement to form a nested if statement.
The statement in an if or if-else statement can be any legal Java statement, including
another if or if-else statement. The inner if statement is said to be nested inside the outer if statement. The inner if statement can contain another if statement; in fact, there is no limit to the depth of the nesting.
代码片段举例:
1.百分制成绩转等级制成绩.
划分标准:90'+:A || 90'~80':B || 80'~70':C || 70'~60':D || 60'- :E.
(除等级A和E外,其他等级均只含下边界不含上边界)
//格式Ⅰif (score >= 90.0) System.out.print("A");else if (score >= 80.0) System.out.print("B"); else if (score >= 70.0) System.out.print("C"); else if (score >= 60.0) System.out.print("D"); else System.out.print("E");
//格式Ⅱif (score >= 90.0) System.out.print("A");else if (score >= 80.0) System.out.print("B");else if (score >= 70.0) System.out.print("C");else if (score >= 60.0) System.out.print("D");else System.out.print("E");
/*格式Ⅱ所呈现的多路if-else语句层次更为清晰,可读性较格式Ⅰ更强,故推荐使用格式Ⅱ*/
对应程序流程图如下:
相关错误代码及其分析:
if (score >= 60.0) System.out.println("D");else if (score >= 70.0) System.out.println("C");else if (score >= 80.0) System.out.println("B");else if (score >= 90.0) System.out.println("A");else System.out.println("E");/* 分析:从条件包含性看,满足等级A、B及C条件的必定满足等级D的条件,故本片段实为将百分制成绩转换为D (60'+)以及E(60'-)两个等级,不符合题意划分五个等级的要求,属于逻辑错误*/
2.身体质量指数(BMI)计算及健康状态评价.
计算公式:BMI=weight/height ².
评价标准:18.5-:偏轻 || 18.5~25.0:正常 || 25.0~30.0:超重 || 30.0+:肥胖.
import java.util.Scanner;public class ComputeAndInterpretBMI{ public static void main(String[] args){ final double UNDERWEIGHT=18.5,NORMAL=25.0,OVERWEIGHT=30.0; Scanner input=new Scanner(System.in); System.out.println("请输入您的体重(Kg):"); double weight=input.nextDouble(); System.out.println("请输入您的身高(m):"); double height=input.nextDouble(); double bmi=weight/Math.pow(height,2); System.out.println("您的身体质量指数是"+bmi); if(bmi<=UNDERWEIGHT) System.out.println("您偏瘦"); else if(bmi<=NORMAL) System.out.println("您体重正常"); else if(bmi<=OVERWEIGHT) System.out.println("您超重了"); else System.out.println("您得减减肥了"); }}
3.试根据搭乘时段及行驶里程给出乘客应付的士费.
出租车价格及收费标准
收费项目
收费标准
3公里以内收费
13元
基本单价
2.3元/公里
空驶费
载客行驶超过15公里部分,基本单价加收50%的费用
夜间收费
23:00(含)至次日5:00(不含)运营时,基本单价加收20%的费用
/** * @see 的士的收费(保留一位小数). * @author JustBeGeek * */import java.util.Scanner;public class TaxiBilling {//起步价 FFP=Flag-Fall Pricepublic static final float FFP=13f;//基本单价 BUP=Basic Unit-Pricepublic static final float BUP=2.3f;//空驶费 NOS=Non-Occupancy Surcharge (%)public static final int NOS=50;//夜间收费 NDS=Night Drive Surcharge (%)public static final int NDS=20;public static final int DAY=0;public static final int NIGHT=1;public static void main(String[] args){Scanner input=new Scanner(System.in);// 时段 PT=Period of Timeint PT=DAY,distance=0;PT=input.nextInt();distance=(int)Math.ceil(input.nextFloat());//行驶里程取整input.close();float bill=0.0f,increm=1.0f;if(distance<3)bill+=FFP;else if(distance<15){if(PT==NIGHT)increm+=NDS/100.0F;bill+=FFP+(distance-3)*BUP*increm;}else{if(PT==NIGHT)increm+=NDS/100.0F;bill+=FFP+12*BUP*increm+(distance-15)*BUP*(increm+NOS/100.0f);}bill=(float) (Math.rint(bill*10)/10.0f);System.out.println(bill);}}
A WORD :All Roads Lead To Rome.
- 嵌套if以及多路if-else语句
- 嵌套的if-else语句
- if..else语句的嵌套
- IF ELSE语句(嵌套)
- IF-ELSE IF-ELSE语句
- 嵌套if-else语句的妙用
- if , if else , if else if , if的嵌套
- if...else嵌套
- Oracle if else if语句
- C#if/if-else/if-else-if语句
- IF...ELSE IF...ELSE IF...ELSE多分枝语句的反汇编判断
- jquery循环语句if-else if-else
- Flex中的if...else if...else语句
- if else if else 语句的拓展
- 级联If -else if -else 语句
- Delphi if else 语句
- if~else 语句再认识
- if else 语句
- 异常com.google.gson.internal.StringMap cannot cast to..原因&解决方案
- 《Learn Python the Hard Way》读书笔记
- java后台接受json数组中嵌套json数组的处理
- cocos2d-x游戏开发屏幕横竖屏切换
- 扫描神器nmap最佳实践使用
- 嵌套if以及多路if-else语句
- UVa 705 - Slash Maze
- java反编译工具的使用
- 取消交互
- 依赖注入的方式和注入的配置实例
- maven+proguard第三方jar打包混淆
- 小记录
- python爬虫
- Laravel5.x启动过程分析