使用布尔运算符

发布于 2024-08-04 15:13:44 字数 309 浏览 6 评论 0原文

这工作正常

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

稀香 2024-08-11 15:13:44

您可能需要查看德摩根定律

1. !((a >= 40 && a <= 50) || (a >= 60 && a <= 80))

2. (!(a >= 40 && a <= 50) && !(a >= 60 && a <= 80))

3. ((!(a >= 40) || !(a <= 50)) && (!(a >= 60) || !(a <= 80))

4. ((a < 40 || a > 50) && (a < 60 || a > 80))


or in other words: (a < 40 || (50 < a && a < 60) || 80 < a)

You might want to look at De Morgan's laws.

1. !((a >= 40 && a <= 50) || (a >= 60 && a <= 80))

2. (!(a >= 40 && a <= 50) && !(a >= 60 && a <= 80))

3. ((!(a >= 40) || !(a <= 50)) && (!(a >= 60) || !(a <= 80))

4. ((a < 40 || a > 50) && (a < 60 || a > 80))


or in other words: (a < 40 || (50 < a && a < 60) || 80 < a)
慈悲佛祖 2024-08-11 15:13:44
if ((a < 40 || a > 50) && (a < 60 || a > 80))
// do something
if ((a < 40 || a > 50) && (a < 60 || a > 80))
// do something
余罪 2024-08-11 15:13:44

虽然我建议弄清楚如何使其正常工作(通过重写),但

if (!((a >= 40 && a <= 50) || (a >= 60 && a <= 80)))

我相信应该有效。

While I would recommend figuring out how to make it work properly (by rewriting it)

if (!((a >= 40 && a <= 50) || (a >= 60 && a <= 80)))

should work I believe.

嘿嘿嘿 2024-08-11 15:13:44

你需要一个“OR”

if ((a < 40 || a > 50) && (a < 60 || a > 80))

或者一个NOT

if (!((a >= 40 && a <= 50) || (a >= 60 && a <= 80)))

You need a "OR"

if ((a < 40 || a > 50) && (a < 60 || a > 80))

Or, a NOT

if (!((a >= 40 && a <= 50) || (a >= 60 && a <= 80)))
秉烛思 2024-08-11 15:13:44

第二个示例

if ((a < 40 && a > 50) || (a < 60 && a > 80))

没有意义,因为 a 不能同时小于 40 和大于 50(或小于 60 和大于 80)。

类似

if (!((a < 40 && a > 50) || (a < 60 && a > 80)))

if ((a < 40 || a > 50) && (a < 60 || a > 80))

You second example

if ((a < 40 && a > 50) || (a < 60 && a > 80))

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

if (!((a < 40 && a > 50) || (a < 60 && a > 80)))

or

if ((a < 40 || a > 50) && (a < 60 || a > 80))
白云不回头 2024-08-11 15:13:44
if (!((a >= 40 && a <= 50) || (a >= 60 && a <= 80)))
if (!((a >= 40 && a <= 50) || (a >= 60 && a <= 80)))
偏爱自由 2024-08-11 15:13:44

假设您想要

if ( not ((a >= 40 && a <= 50) || (a >= 60 && a <= 80)) )

then 的等价物,如果您考虑原始表达式,它应该是

if (a < 40 || (a > 50 && a < 60) || a > 80)

第一个表达式允许 a 是从 40 到 50 或从60 到 80。在英语中否定它,你想要一个小于 40 或介于 50 到 60 或大于 80 的数字。

德摩根定律可以给你一个准确的答案,但我更喜欢你可以大声朗读并理解的代码。

Assuming that you want the equivalent of

if ( not ((a >= 40 && a <= 50) || (a >= 60 && a <= 80)) )

then, if you think about the original expression, it should be

if (a < 40 || (a > 50 && a < 60) || a > 80)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文