繁体   English   中英

if 语句和它应该写的顺序

[英]if statements and the order it should be written

我需要开发一种算法来计算机票的价格。 首先,用户必须输入价格、座位位置(上 = 30% 折扣,中间 = 普通,下 = 25% 溢价)和客户类型(高级 = 10% 折扣,成人 = 普通)。

所以做一个样例测试:用户输入50为价格,Upper为位置,Senior为客户类型,最终票价应为31.50。

到目前为止,我的算法得到了 35。有人能找出我的错误吗?

    if (cusType.charAt(0) == 'A'){
        ticPrice = regTicPrice;

        if (location.charAt(0) == 'U'){
            ticPrice = regTicPrice * 0.7;
        }
        else if (location.charAt(0) == 'L'){
            ticPrice = regTicPrice * 1.25;
        }
        else if (location.charAt(0) == 'M'){
            ticPrice = regTicPrice;
        }
    }

    else if (cusType.charAt(0) == 'S'){
        ticPrice = regTicPrice * 0.9;

        if (location.charAt(0) == 'U'){
            ticPrice = regTicPrice * 0.7;
        }
        else if (location.charAt(0) == 'L'){
            ticPrice = regTicPrice * 1.25;
        }
        else if (location.charAt(0) == 'M'){
            ticPrice = regTicPrice;
        }
    }
    else{
     ticPrice = 0;   
    }

使用HashMap让你的生活更简单,就像这样:

HashMap customerTypeDiscount = new HashMap<String, Double>();
customerTypeDiscount.put("Adult", 1);
customerTypeDiscount.put("Senior", 0.9);

HashMap locationDiscount = new HashMap<String, Double>();
locationDiscount.put("Upper", 0.7);
locationDiscount.put("Middle", 1);
locationDiscount.put("Lower", 1.25);

ticPrice = regTicPrice * ((Double) customerTypeDiscount.get(cusType)) * (((Double) locationDiscount.get(location));

您的问题与您更新 ticPrice 相关,但在成为高级会员后从不重复使用更新后的 ticPrice。

你最终为“U”做 50 * .7 而不是 50 * .9 * .7。

if (cusType.charAt(0) == 'A'){
    ticPrice = regTicPrice;

    if (location.charAt(0) == 'U'){
        ticPrice = ticPrice * 0.7;
    }
    else if (location.charAt(0) == 'L'){
        ticPrice = ticPrice * 1.25;
    }
    else if (location.charAt(0) == 'M'){
        // you don't technically need this if statement at the end since it doesnt change
        ticPrice = ticPrice;
    }
}

else if (cusType.charAt(0) == 'S'){
    ticPrice = ticPrice * 0.9;

    if (location.charAt(0) == 'U'){
        ticPrice = ticPrice * 0.7;
    }
    else if (location.charAt(0) == 'L'){
        ticPrice = ticPrice * 1.25;
    }
    else if (location.charAt(0) == 'M'){
        // you dont technically need this if statement at the end since it doesnt change
        ticPrice = ticPrice;
    }
}
else{
 ticPrice = 0;   
}

您在计算机票的折扣价时使用的是机票的实际价格,请在 Inner If 中使用 ticPrice 而不是下面的 regTicPrice sa :

   if (cusType.charAt(0) == 'A'){
    ticPrice = regTicPrice;
    if (location.charAt(0) == 'U'){
        ticPrice = ticPrice * 0.7;
    }
    else if (location.charAt(0) == 'L'){
        ticPrice = ticPrice * 1.25;
    }
    else if (location.charAt(0) == 'M'){
        ticPrice = ticPrice;
    }
}

else if (cusType.charAt(0) == 'S'){
    ticPrice = regTicPrice * 0.9;

    if (location.charAt(0) == 'U'){
        ticPrice = ticPrice * 0.7;
    }
    else if (location.charAt(0) == 'L'){
        ticPrice = ticPrice * 1.25;
    }
    else if (location.charAt(0) == 'M'){
        ticPrice = ticPrice;
    }
}
else{
 ticPrice = 0;   
}

检查您编写的这行代码:

ticPrice = regTicPrice * 0.9;

    if (location.charAt(0) == 'U'){
        ticPrice = regTicPrice * 0.7;
    }

regTicPrice 是 50

ticPrice =  regTicPrice * 0.9

现在 ticPrice 的值 = 50 * 0.9 = 45

下一行是

ticPrice = regTicPrice * 0.7
Wrong ==== ticPrice = 50 * 0.7 = 35
Right ==== ticPrice = 45 * 0.7 = 31.50

您正在覆盖 ticPrice: with regTicPrice * 0.7

代码应该如下所示

ticPrice = regTicPrice * 0.9;

    if (location.charAt(0) == 'U'){
        ticPrice = ticPrice * 0.7;
    }

您必须为ticPrice使用float数据类型,如果您已经在使用它,请尝试ticPrice=(float)ticPrice * 0.9;

重构和修复

ticPrice = 0; 

if (cusType.charAt(0) == 'A')
    ticPrice = regTicPrice;
else if (cusType.charAt(0) == 'S')
    ticPrice = regTicPrice * 0.9;

if (ticPrice > 0) {
    if (location.charAt(0) == 'U')
        ticPrice = ticPrice * 0.7;
    else if (location.charAt(0) == 'L')
        ticPrice = ticPrice * 1.25;
}

使用 hashmap 存储您的信息并从这里选择

if (cusType.charAt(0) == 'A'){ ticPrice = regTicPrice;

    if (location.charAt(0) == 'U'){
        ticPrice = ticPrice * 0.7;      //you made mistake here..
    }
    else if (location.charAt(0) == 'L'){
        ticPrice = ticPrice * 1.25;
    }
    else if (location.charAt(0) == 'M'){
        ticPrice = regTicPrice;
    }
}

else if (cusType.charAt(0) == 'S'){
    ticPrice = regTicPrice * 0.9;

    if (location.charAt(0) == 'U'){
        ticPrice = ticPrice * 0.7;
    }
    else if (location.charAt(0) == 'L'){
        ticPrice = ticPrice * 1.25;
    }
    else if (location.charAt(0) == 'M'){
        ticPrice = regTicPrice;
    }
}
else{
 ticPrice = 0;   
}

一旦你得到了可爱的类型(高级):50*0.9=45=ticPrice。 现在你又需要对 loaction(Upper) 应用折扣:45*0.7=31.5.=> ticPrice*0.7。 不是 regTicPrice*0.7

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM