C# 理解 bool 表达式

发布于 2024-11-17 02:10:05 字数 395 浏览 4 评论 0原文

我确信这是一个非常简单的问题,但我无法弄清楚为什么这个断言失败......

基本上如果 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 技术交流群。

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

发布评论

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

评论(9

恍梦境° 2024-11-24 02:10:05

你说的话毫无意义。

这是我(和编译器)如何理解你所说的:

基本上,如果 IsApple 为 false 或 IsBannana 为 false,则断言应该失败

  • 如果 IsApple 为 false,则断言应该失败
  • 如果 IsBanana 为 false,则断言应该失败

换句话说,如果其中一个为 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:

basically if IsApple is false or IsBannana is false assertion should fail

  • If IsApple is false the assertion should fail
  • If IsBanana is false, the assertion should fail

In other words, if one of them is false, you don't care whether or not the other is also false.

however if one of the two is true assertion should pass

  • If one of them is true, you don't care whether the other one is also true.

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.

∞琼窗梦回ˉ 2024-11-24 02:10:05

!IsBannana 为 true,因此 if 的计算结果为 true。

我打赌你想要:

if(!IsApple && !IsBananna)
    Assert.Fail();

!IsBannana is true so the if evaluates to true.

I bet you wanted:

if(!IsApple && !IsBananna)
    Assert.Fail();
墟烟 2024-11-24 02:10:05

如果其中一个为假,则断言将失败,只有当两个都为真时,断言才会通过。

编辑:我将其重写为:

if (IsApple || IsBanana)
{
    Assert.Pass();
}
else
{
    Assert.Fail();
}

有趣的是,所提出的问题无法回答(因为根据问题,如果一个为真而另一个为假,则预期结果是不明确的)。

The assert will fail if EITHER is false, it will only pass if BOTH are true.

Edit: I would re-write this as:

if (IsApple || IsBanana)
{
    Assert.Pass();
}
else
{
    Assert.Fail();
}

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).

美人迟暮 2024-11-24 02:10:05

改为这样做:

if(!IsApple && !IsBannana)

Do this instead:

if(!IsApple && !IsBannana)
悲念泪 2024-11-24 02:10:05
        if (IsApple != IsBannana)
            Assert.Fail();

我认为这个技巧 != 是一个 C# 常见问题解答,就像穷人的 XOR

        if (IsApple != IsBannana)
            Assert.Fail();

I think this trick != is a C# FAQ as poor man's XOR

-柠檬树下少年和吉他 2024-11-24 02:10:05

你不希望出现 a 和 b 都不为假的情况,或者至少有一个应该为真,所以

   [Test]      
    public void IsApplesOrBannans() 
    {      
       bool IsApple = true;     
       bool IsBannana = false;    
       if (!(IsApple || IsBannana))   
            Assert.Fail();        
       Assert.Pass();       
    }

You dont want the case where neither a nor b is false, or rather at least one should be true, so

   [Test]      
    public void IsApplesOrBannans() 
    {      
       bool IsApple = true;     
       bool IsBannana = false;    
       if (!(IsApple || IsBannana))   
            Assert.Fail();        
       Assert.Pass();       
    }
清旖 2024-11-24 02:10:05

您的通过条件是 IsApple || IsBannana,因此您的失败条件可以写为:

if (!(IsApple || IsBannana))

或者您也可以说两者都必须为假才能失败:

if ((IsApple==false) && (IsBannana==false))

通常写为:

if (!IsApple && !IsBannana))

就我个人而言,我认为这三种选择中的第一种是最好的。但这只是一种风格选择。在功能上它们是等效的。

或者更好的是,您可以切换 ifthenelse 部分:

public void IsApplesOrBannans()
{
    bool IsApple = true;
    bool IsBannana = false;

    if (IsApple || IsBannana)
        Assert.Pass();
    else
        Assert.Fail();            
}

或者甚至扔掉 if完整声明:

public void IsApplesOrBannans()
{
    bool IsApple = true;
    bool IsBannana = false;

    Assert.IsTrue(IsApple || IsBannana);
}

Your pass condition is IsApple || IsBannana, so your fail condition can be written as:

if (!(IsApple || IsBannana))

Or alternatively you can say both must be false to fail:

if ((IsApple==false) && (IsBannana==false))

Which usually is written as:

if (!IsApple && !IsBannana))

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 and else part of the if:

public void IsApplesOrBannans()
{
    bool IsApple = true;
    bool IsBannana = false;

    if (IsApple || IsBannana)
        Assert.Pass();
    else
        Assert.Fail();            
}

Or even throw out the if statement entirely:

public void IsApplesOrBannans()
{
    bool IsApple = true;
    bool IsBannana = false;

    Assert.IsTrue(IsApple || IsBannana);
}
好多鱼好多余 2024-11-24 02:10:05

您的通过条件与失败条件相矛盾,因为如果只有一个为真,则另一个为假,从而满足失败条件。

如果你想让它有意义,请将其更改为:

if (!IsApple && !IsBanana)
    Assert.Fail():

或者,如果你也不希望它同时成为苹果和香蕉

if (IsApple == IsBanana)
    Assert.Fail():

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:

if (!IsApple && !IsBanana)
    Assert.Fail():

or, if you also don't want it to be an apple and banana at the same time

if (IsApple == IsBanana)
    Assert.Fail():
随风而去 2024-11-24 02:10:05
(!A|| !A) = !!(!A || !B) = !(A && B) = !(true && false) --> false
(!A|| !A) = !!(!A || !B) = !(A && B) = !(true && false) --> false
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文