验证真值表的最少检查次数
我有一个java程序,我想验证3个布尔值中的任何一个是否为假。我想找出可以编写的最小表达式来检查排列。
if(!(needsWork && (needsApproval || isAdmin) ))
我认为这足以确保如果 3 个布尔值中的任何一个为 false,我想停止处理。然而,我偷偷怀疑我错过了一些东西。
I have a java program where I want to validate if any of 3 booleans is false. I want to figure out the smallest expression I can write to check against the permutations.
if(!(needsWork && (needsApproval || isAdmin) ))
I think this is enough to make sure that if any of the 3 booleans is false I want to stop processing. However, I have the sneaking suspicion I am missing something.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
if (!needsWork || !needsApproval || !isAdmin)
不起作用吗? Java 支持短路评估。Would
if (!needsWork || !needsApproval || !isAdmin)
not work? Java supports short-circuit evaluation.由于
和
具有不同的真值表,您确定不同的情况并不重要吗?
转换
我经常使用布尔表达式来尝试澄清或简化它们,并且我使用这些逻辑转换来帮助我:
当然还有很多其他的转换,但这些是我最常使用的。它可能看起来很复杂,但是一旦你开始练习它们就很容易记住它们。
在你的情况下,如果你想看看一些可能的等效语句:
这里有一些转换:
但我没有看到你所拥有的任何真正的简化。
当然,如果检查
3 个布尔值中的任何一个为 false
都可以,那么您的选择就很简单Since
and
have different truth tables, are you sure that the cases that are different don't matter?
Transformations
I will often play around with boolean expressions to try to clarify or simplify them and I use these logic transformations to help me:
There are of course many others, but these are ones I use most often. It may look complicated, but they are easy to get to know by heart once you start by practicing them.
In your case, if you wanted to see what some possible equivalent statements for:
here are some transformations:
but I don't see any real simplification of what you have.
Of course, if checking that
any of 3 booleans are false
is fine, then your choices are simple