嵌套if以及多路if-else语句

说明:

    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-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 Price
	public static final float FFP=13f;
	//基本单价  BUP=Basic Unit-Price
	public 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 Time
		int 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.





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值