Java:使int和boolean在“中具有可比性”;如果;声明
有没有可能有一种说法,比如Java:使int和boolean在“中具有可比性”;如果;声明,java,boolean,Java,Boolean,有没有可能有一种说法,比如 if(delco == 1 && heavy < 5) System.out.println("The total cost of your delivery is: $" + OPT_ONE); if(delco == 1 && heavy >= 5 && heavy <= 20) System.out.println("The total cos
if(delco == 1 && heavy < 5)
System.out.println("The total cost of your delivery is: $" + OPT_ONE);
if(delco == 1 && heavy >= 5 && heavy <= 20)
System.out.println("The total cost of your delivery is: $" + OPT_TWO);
if(delco==1&&heavy<5)
System.out.println(“交付的总成本为:$”+OPT_ONE);
如果(delco==1&&heavy>=5&&heavy=5&&heavy您只是遗漏了一些括号,因为您的逻辑似乎正常。它应该是,例如:
if ( (delco == 1 && heavy < 5) && (overnightShip == YES) )
...
在这种情况下,这些括号也是多余的,整个过程简化为:
if ( delco == 1 && heavy < 5 && overnightShip )
...
if(德尔科==1和重型<5和过夜船)
...
只需单独使用布尔值即可:
if (delco == 1 && heavy < 5 && overnightShip)
if(德尔科==1和重型<5和过夜船)
将布尔“标志”变量与布尔常量进行比较是一种糟糕的风格-始终更喜欢按原样测试布尔值。Java中的布尔类型具有以下值:
true
false
不是:
除非你自己在某个地方定义了这些常量
因此,您的代码应该如下所示:
(overnightShip == true)
(overnightShip == false)
甚至:
(overnightShip) // true
(! overnightShip) // false
夜航
、是
和否
的类型是什么?此外,如果条件满足,则在整个条件周围加上括号。
YES
NO
(overnightShip == true)
(overnightShip == false)
(overnightShip) // true
(! overnightShip) // false