是否可以发表这样的声明?

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


...这也适用布尔逻辑来表示输出吗?像这样

  boolean overnight;

     if(delco == 1 && heavy < 5) && (overnightShip == YES)
         System.out.println("The total cost of your delivery is: $" + OPT_ONE + OVERNIGHT);
     if(delco == 1 && heavy >= 5 && heavy <= 20) && (overnightShip == NO)
        System.out.println("The total cost of your delivery is: $" + OPT_TWO);


我已经尝试了这段代码的一些变体,并且收到了错误消息,指出它们是无法比拟的类型。如何使它们具有可比性?

最佳答案

您只是错过了一些括号,因为您的逻辑似乎还行。应该是,例如:

if ( (delco == 1 && heavy < 5) && (overnightShip == YES) )
    ...


注意外括号。

还要假设您已将YES定义为等于true的布尔常量,并且这是多余的,所以:

if ( (delco == 1 && heavy < 5) && (overnightShip) )
    ...


在这种情况下,这些括号也是多余的,整个过程简化为:

if ( delco == 1 && heavy < 5 && overnightShip )
    ...

关于java - Java:在“if”语句中使int和boolean具有可比性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22725482/

10-10 23:51