我一直坚持的部分是
boolean 值(0 % 2 !=0)
== 错误。我的意思是,如果 2 进入 0,0 次,那么余数就是 2,而 2 不等于 0。所以它应该是真的。然而,当我将 boolean 值放入我的 java 程序时,它会将其视为 false。有人知道为什么吗?
我能想到的唯一合乎逻辑的答案是,也许整数进入 0 和无数次,因此被识别为假,有人吗?
最佳答案
有两个步骤:
0 % 2
的计算结果为0
。0 != 0
的计算结果为false
。
为了详细说明第一步,JLS defines %
运算符是这样的:
The binary % operator is said to yield the remainder of its operands from an implied division; the left-hand operand is the dividend and the right-hand operand is the divisor.
0
除以 2
的余数是 0
而不是您认为的 2
。
关于java - `java (0 % 2 != 0) == false`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10511170/