使用布尔运算符
这工作正常
if ((a >= 40 && a <= 50) || (a >= 60 && a <= 80))
// do something
我该如何做相反的事情?
if ((a < 40 && a > 50) || (a < 60 && a > 80))
// do something
该代码未按预期工作。我想要类似如果没有(条件)
This works fine
if ((a >= 40 && a <= 50) || (a >= 60 && a <= 80))
// do something
How do I do the reverse of it?
if ((a < 40 && a > 50) || (a < 60 && a > 80))
// do something
The code does not work as expected. I want something like if not (condition)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可能需要查看德摩根定律。
You might want to look at De Morgan's laws.
虽然我建议弄清楚如何使其正常工作(通过重写),但
我相信应该有效。
While I would recommend figuring out how to make it work properly (by rewriting it)
should work I believe.
你需要一个“OR”
或者一个NOT
You need a "OR"
Or, a NOT
第二个示例
没有意义,因为
a
不能同时小于 40 和大于 50(或小于 60 和大于 80)。类似
或
You second example
doesn't make sense as
a
cannot be both less than 40 and greater than 50 (or less than 60 and greater than 80) at the same time.Something like
or
假设您想要
then 的等价物,如果您考虑原始表达式,它应该是
第一个表达式允许
a
是从 40 到 50 或从60 到 80。在英语中否定它,你想要一个小于 40 或介于 50 到 60 或大于 80 的数字。德摩根定律可以给你一个准确的答案,但我更喜欢你可以大声朗读并理解的代码。
Assuming that you want the equivalent of
then, if you think about the original expression, it should be
The first expression is allowing
a
to be a number from 40 to 50 or from 60 to 80. Negate that in English and you want a number less than 40 or between 50 and 60 or greater than 80.De Morgan's Laws can get you an accurate answer, but I prefer code that you can read aloud and make sense of.