将这个 if-then 逻辑转换为布尔表达式?
我在使这段代码更简洁(最好是单个布尔表达式)方面有点费脑子,
这是我的代码:
if (d.Unemployed)
{
if (type.Unemployed)
{
tmp.Unemployed = true;
}
else
{
tmp.Unemployed = false;
}
}
else
{
if (type.Unemployed)
{
tmp.Unemployed = false;
}
else
{
tmp.Unemployed = true;
}
}
基本上要点是,如果 type
或 d 没有失业,那么
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
怎么样:
How about:
如果我们按照代码构建真值表,我们得到
上面等价于
xor
运算的求反。如果语言没有
xor
运算符,我们可以对布尔值使用!=
。If we construct a truth table by following the code, we get
The above is equivalent with the negation of the
xor
operation.If the language doesn't have the
xor
operator we can use the!=
on boolean values.想想这给你带来了多少“脑放屁”,我会考虑使用一个命名良好的变量来避免将来再次经历这个心理过程。像这样的东西:
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:
上述代码的意思是“都失业或者都没有失业”。因此,不是 (A xor B):
The above code means "both unemployed or both not unemployed". Thus, not (A xor B):