Why does my boolean test in java always fail?

后端 未结 6 1407
别跟我提以往
别跟我提以往 2021-01-21 21:59

I am trying to make a boolean test so that if one of the tire pressures is below 35 or over 45 the system outputs \"bad inflation\".

In my class I must use a boolean, wh

6条回答
  •  花落未央
    2021-01-21 22:29

    Usually, code like if (variable = constantValue) is treated as compilation error in Java. However, there is an exception when the constant value is a boolean. In that case, the statement is equal to if (constantValue). This kind of issue can not be found in the compilation phase.

    So, I suggest 1)do not compare with boolean constant value, just do it by if (booleanVar); 2)always put the constant value ahead, like 'if (true = variable)' will cause the compilation fail.

提交回复
热议问题