if语句及其编写顺序

时间:2018-02-07 05:08:01

标签: java if-statement

我需要开发一种算法来计算机票的价格。首先,用户必须输入价格,座位位置(Upper = 30%off,Middle = regular,Lower = 25%premium)和客户类型(Senior = 10%off,Adult = regular)。

所以样本测试:用户输入50表示价格,上限表示位置,高级表示客户类型,最终票价应为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;   
    }

8 个答案:

答案 0 :(得分:1)

使用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));

答案 1 :(得分:0)

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

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

做50 * .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;   
}

答案 2 :(得分:0)

您在计算机票折扣价时使用的是实际票价,请使用内部的ticPrice而不是下面的regTicPrice:

   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;   
}

答案 3 :(得分: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:使用regTicPrice * 0.7

代码应该如下所示

ticPrice = regTicPrice * 0.9;

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

答案 4 :(得分:0)

您必须使用float的{​​{1}}数据类型,如果您已经在使用它,请尝试ticPrice

答案 5 :(得分:0)

重构并修复

ticPrice=(float)ticPrice * 0.9;

答案 6 :(得分:0)

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

答案 7 :(得分:0)

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;   
}

获得Cutsome类型(高级)后:50 * 0.9 = 45 = ticPrice。 现在再次需要对loaction(Upper)应用折扣:45 * 0.7 = 31.5。=&gt; ticPrice * 0.7。不是regTicPrice * 0.7