英文:
Simple Boolean Logic
问题
我试图确定以下表达式在何种条件下会求值为 false
:其中 a
和 b
是已经正确声明的布尔变量:
(a && (b || !a)) == a && b
在我看来,这个表达式似乎总是会求值为 true。如果 a
或者 b
有一个为 false
,等式两边都会求值为 false
。如果 a
和 b
都为 true,那么两边都会求值为 true。这就是所有的情况,而且这也是我在线作业的正确答案。然而,当我在带有Java 11 JVM的IntelliJ CE中运行这个表达式时,无论 b
为何,它似乎总是打印 false
:
当 a 和 b 都为 false 时,IntelliJ 输出 false
当 b
为 false 而 a
为 true 时,我得到相同的输出。有人可以解释一下我的逻辑错误在哪里吗?非常感谢。
英文:
I'm trying to determine the conditions under which the following expression, where a
and b
are properly declared boolean variables, evaluates to false
:
(a && (b || !a)) == a && b
To me, it seems that this expression will always evaluate to true. If either a
or b
is false
, both sides of the equality operator will evaluate to false
. If a
and b
are both true, then both sides will evaluate to true. That's all the options, and it's the correct answer for my online homework. However, when I run this in IntelliJ CE with the Java 11 JVM, it seems like it prints false whenever b
is false:
when a and b are both false, IntelliJ outputs false
I get the same output when b is false and a is true. Can someone explain where the fault in my logic is? Thank you very much.
答案1
得分: 3
我认为它优先考虑了 ==
操作,而不是 &&
。
尝试这样写 -
(a && (b || !a)) == (a && b)
英文:
I think it is giving ==
operation priority over &&
Try this -
(a && (b || !a)) == (a && b)
答案2
得分: 2
你的代码应该是:
boolean c = (a && (b || !a)) == (a && b);
否则它将被解释为:
boolean c = ((a && (b || !a)) == a) && b;
英文:
You code should be:
boolean c = (a && (b || !a)) == (a && b);
otherwise it is evaluated as:
boolean c = ((a && (b || !a)) == a) && b;
答案3
得分: -1
布尔运算具有等价的数学运算(这就是 CPU 最终执行的操作)。您可以使用这种方法检查任何方程,以确保其输出与您的预期相符。
这可以帮助您迅速判断您的布尔逻辑在所有情况下是否正确。
使用您的方程,这里有一个示例:
将 "true" 替换为 1,将 "false" 替换为 0。将 && 替换为 *(表示乘法),将 || 替换为 +(表示加法)。! 仍然表示取反。然后检查相等性,
因此
a*(b+!a) == a*b
然后当 a = false(0),b = false(0) 时,我们有
0*(0+1) == 0*0
或者 0=0
这是真的。
我们可以继续进行其他选项的检查。
a=1,b=1
1*(1+0) == 1*1
或者 1=1
同样,是真的。
a=1,b=0
1*(0+1) == 1*0
1*1=0?
不正确(假的)。
通过最后一个测试,我们可以看到这个方程并不总是评估为真。就像数学中一样,布尔运算也有运算顺序(正如许多其他人所提到的),但我喜欢使用这种方法来确保我的方程按预期工作。
英文:
Boolean operations have equivalent mathematical operations (this is what the CPU is ultimately doing). You can use this method to check any equation to make sure that it is coming out how you expect.
It can help you quickly see if your boolean logic is correct for all cases.
Using your equation here is an example:
Replace "true" with 1 and "false" with 0. Replace && with * (as in multiplies) and || with + (as in add). ! does what you expect still. Then check equality
so
a*(b+!a) == a*b
Then when a = false (0) and b = false(0) we have
0*(0+1) == 0*0
or 0=0
Which is true.
We can keep going with the other options to check.
a=1, b=1
1*(1+0) == 1*1
or 1=1
again, true
a=1, b=0
1*(0+1) == 1*0
1*1=0?
Not correct (false).
With that last test, we can see that this equation does not always evaluate to true. Just like in math, boolean operations have an order of operations (as mentioned by many others), but I like using this method to make sure my equations are working as intended.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论