将这个 if-then 逻辑转换为布尔表达式?

发布于 2024-11-23 15:47:49 字数 891 浏览 3 评论 0原文

我在使这段代码更简洁(最好是单个布尔表达式)方面有点费脑子,

这是我的代码:

                    if (d.Unemployed)
                    {
                        if (type.Unemployed)
                        {
                            tmp.Unemployed = true;
                        }
                        else
                        {
                            tmp.Unemployed = false;
                        }
                    }
                    else
                    {
                        if (type.Unemployed)
                        {
                            tmp.Unemployed = false;
                        }
                        else
                        {
                            tmp.Unemployed = true;
                        }
                    }

基本上要点是,如果 typed 没有失业,那么 tmp 应设置为未失业。

I'm having a bit of a brain fart on making this code more concise(preferably a single boolean expression)

This is my code:

                    if (d.Unemployed)
                    {
                        if (type.Unemployed)
                        {
                            tmp.Unemployed = true;
                        }
                        else
                        {
                            tmp.Unemployed = false;
                        }
                    }
                    else
                    {
                        if (type.Unemployed)
                        {
                            tmp.Unemployed = false;
                        }
                        else
                        {
                            tmp.Unemployed = true;
                        }
                    }

Basically the point is that if either type or d is not unemployed, then tmp should be set to not unemployed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

桃酥萝莉 2024-11-30 15:47:49

怎么样:

tmp.Unemployed = type.Unemployed == d.Unemployed;

How about:

tmp.Unemployed = type.Unemployed == d.Unemployed;
醉生梦死 2024-11-30 15:47:49

如果我们按照代码构建真值表,我们得到

d  | type | tmp
---+------+----
1  |   1  |  1
---+------+----
1  |   0  |  0
----+-----+----
0  |   1  |  0
----+-----+----
0  |   0  |  1

上面等价于xor运算的求反。

tmp = not (d xor type)

如果语言没有xor 运算符,我们可以对布尔值使用!=

tmp = ! (d != type);
// or
tmp = d == type;

If we construct a truth table by following the code, we get

d  | type | tmp
---+------+----
1  |   1  |  1
---+------+----
1  |   0  |  0
----+-----+----
0  |   1  |  0
----+-----+----
0  |   0  |  1

The above is equivalent with the negation of the xor operation.

tmp = not (d xor type)

If the language doesn't have the xor operator we can use the != on boolean values.

tmp = ! (d != type);
// or
tmp = d == type;
没企图 2024-11-30 15:47:49

想想这给你带来了多少“脑放屁”,我会考虑使用一个命名良好的变量来避免将来再次经历这个心理过程。像这样的东西:

isTmpUnemployed = (type.Unemployed == d.Unemployed);
tmp.Unemployed = isTmpUnemployed;

Thinking about how much "brain fart" this caused you I would consider using a well named variable to avoid having to go through this mental process again in future. Something like this:

isTmpUnemployed = (type.Unemployed == d.Unemployed);
tmp.Unemployed = isTmpUnemployed;
我早已燃尽 2024-11-30 15:47:49

上述代码的意思是“都失业或者都没有失业”。因此,不是 (A xor B):

 tmp.Unemployed = ! ( D.Unemployed ^ type.Unemployed)

The above code means "both unemployed or both not unemployed". Thus, not (A xor B):

 tmp.Unemployed = ! ( D.Unemployed ^ type.Unemployed)
〃安静 2024-11-30 15:47:49
tmp.Unemployed = d.Unemployed || type.Unemployed ? !tmp.Unemployed : null;
tmp.Unemployed = d.Unemployed || type.Unemployed ? !tmp.Unemployed : null;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文