C# 理解 bool 表达式
我确信这是一个非常简单的问题,但我无法弄清楚为什么这个断言失败......
基本上如果 IsApple 为假或 IsBannana 为假断言应该失败,但是如果两者之一为真断言应该通过,有人可以解释吗为什么这个断言失败了?
[Test]
public void IsApplesOrBannans()
{
bool IsApple = true;
bool IsBannana = false;
if (!IsApple || !IsBannana)
Assert.Fail();
Assert.Pass();
}
This is a really simple question I am sure, but I cannot figure out why this assertion fails...
basically if IsApple is false or IsBannana is false assertion should fail, however if one of the two is true assertion should pass, could anyone explain why this assertain fails?
[Test]
public void IsApplesOrBannans()
{
bool IsApple = true;
bool IsBannana = false;
if (!IsApple || !IsBannana)
Assert.Fail();
Assert.Pass();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
你说的话毫无意义。
这是我(和编译器)如何理解你所说的:
换句话说,如果其中一个为 false,则您不在乎 em> 无论其他是否也是错误的。
这些要求相互矛盾。
也许你的意思是“如果 IsApple 为假 AND IsBanana 为假”;也就是说,如果它们都是假的。
但你写的是“如果 IsApple 为假OR IsBanana 为假”,也就是说,如果其中一个为假。
What you're saying makes no sense.
Here's how I (and the compiler) understand what you're saying:
In other words, if one of them is false, you don't care whether or not the other is also false.
Those requirements contradict each others.
Perhaps you meant "if IsApple is false AND IsBanana is false"; that is, if they are both false.
But what you wrote was "if IsApple is false OR IsBanana is false", that is, if one of them are false.
!IsBannana
为 true,因此 if 的计算结果为 true。我打赌你想要:
!IsBannana
is true so the if evaluates to true.I bet you wanted:
如果其中一个为假,则断言将失败,只有当两个都为真时,断言才会通过。
编辑:我将其重写为:
有趣的是,所提出的问题无法回答(因为根据问题,如果一个为真而另一个为假,则预期结果是不明确的)。
The assert will fail if EITHER is false, it will only pass if BOTH are true.
Edit: I would re-write this as:
Interestingly, the question as posed cannot be answered (since if one is true and the other is false according to the question the expected result is amiguous).
改为这样做:
Do this instead:
我认为这个技巧
!=
是一个 C# 常见问题解答,就像穷人的 XORI think this trick
!=
is a C# FAQ as poor man's XOR你不希望出现 a 和 b 都不为假的情况,或者至少有一个应该为真,所以
You dont want the case where neither a nor b is false, or rather at least one should be true, so
您的通过条件是
IsApple || IsBannana
,因此您的失败条件可以写为:或者您也可以说两者都必须为假才能失败:
通常写为:
就我个人而言,我认为这三种选择中的第一种是最好的。但这只是一种风格选择。在功能上它们是等效的。
或者更好的是,您可以切换
if
的then
和else
部分:或者甚至扔掉
if
完整声明:Your pass condition is
IsApple || IsBannana
, so your fail condition can be written as:Or alternatively you can say both must be false to fail:
Which usually is written as:
Personally I think the first of these three alternatives is the best. But that's just a stylistic choice. functionally they are equivalent.
Or even better, you could switch your
then
andelse
part of theif
:Or even throw out the
if
statement entirely:您的通过条件与失败条件相矛盾,因为如果只有一个为真,则另一个为假,从而满足失败条件。
如果你想让它有意义,请将其更改为:
或者,如果你也不希望它同时成为苹果和香蕉
Your pass condition contradicts your fail condition, because if only one is true, the other one is false and thus fulfills the fail condition.
if you want it to make sense, change it to this:
or, if you also don't want it to be an apple and banana at the same time