我一直在为我的单一课程编写一个程序而且我对可能很荒谬的事情感到难过。
这是我的一些代码
// International Calls Selection
System.out.println("Would you like to include international calls?");
System.out.println("1. Include");
System.out.println("2. Do not Include");
int intCallChoice = keyboard.nextInt();
boolean intChoice = true;
if (intCallChoice == 2)
{
intChoice = false;
}
这部分接受用户的输入,然后在我的合约类中,我有一个价格计算器方法,如下所示:
if (intChoice = true)
{
priceOfContract *= 1.15;
}
然而,无论我选择加入国际电话还是不包括国际电话,结果总是会增加15%?
答案 0 :(得分:0)
if (intChoice == true)
将永远为真,因为您使用的是赋值(single =)而不是equals(double ==)
你的病情应该是
if (intChoice) // for boolean check
或只是
{{1}}
答案 1 :(得分:0)
尝试使用==
代替=
,如下所示:
if (intChoice == true){
=
用于分配,但==
用于检查相等性。
或者只使用:
if (intChoice){
答案 2 :(得分:0)
正如其他人所说,你需要使用==
来比较值,但是因为你的变量是一个布尔值,你可以进一步缩短这个if语句:
if (intChoice) {
priceOfContract *= 1.15;
}
答案 3 :(得分:0)
if (intChoice = true)
是一个条件,当你想要将intChoice变量值与true进行比较时,你必须使用两个等号(==),就像你在下面那样:
if (intCallChoice == 2)
如果只使用一个等号,则将intChoice变量的值更改为true,条件将全部为
if(true)
对于任何类型的调用,这意味着priceOfContract将乘以1.15。