C# 国际单项体育联合会声明及&;或||

C# 国际单项体育联合会声明及&;或||,c#,if-statement,logical-operators,C#,If Statement,Logical Operators,好吧,我是C#的初学者,我很难理解下面的if语句 对于本例,INumber声明为8,dPrice为0,dTAX为10 if (iNumber != 8 || iNumber != 9) { dPrice = dPrice + dTAX; } 有人能解释一下为什么它要输入声明并将dTAX中的10添加到dPrice中吗 我知道将其更改为&&works,但为什么 据我所知,它应该只输入If语句,如果iNumber不等于8或9,那

好吧,我是C#的初学者,我很难理解下面的if语句

对于本例,INumber声明为8,dPrice为0,dTAX为10

        if (iNumber != 8 || iNumber != 9)
        {
            dPrice = dPrice + dTAX;
        }
有人能解释一下为什么它要输入声明并将dTAX中的10添加到dPrice中吗

我知道将其更改为&&works,但为什么

据我所知,它应该只输入If语句,如果iNumber不等于8或9,那么它就不应该输入

下面是我在通过| | if语句运行后的输出

        Inumber is: 8

        dPrice was: 0
        dPrice is now: 10

        dTAX is: 10

有人能给我解释一下吗?

如果iNumber不等于8或者iNumber不等于9,它应该只输入If语句。它不等于9,因此将输入

如果iNumber不等于8或iNumber不等于9,则只应输入If语句。它不等于9,因此将输入逻辑AND(&&)

如果两个操作数都为true,逻辑AND运算符(&&)返回布尔值true,否则返回false。操作数在求值之前隐式转换为bool类型,结果为bool类型。逻辑AND具有从左到右的关联性

逻辑或(| |)

如果其中一个或两个操作数为真,逻辑OR运算符(| |)将返回布尔值true,否则返回false。操作数在求值之前隐式转换为bool类型,结果为bool类型。逻辑OR具有从左到右的关联性

因此,如果你有:

bool someVariable = true;
bool someOtherVariable = false;

if ((someVariable == true) && (someOtherVaribale == true))
{
    //This code will be executed
}

if ((someVaribale == true) || (someOtherVariable == true))
{
    //This code will be executed
}
逻辑AND(&&)

如果两个操作数都为true,逻辑AND运算符(&&)返回布尔值true,否则返回false。操作数在求值之前隐式转换为bool类型,结果为bool类型。逻辑AND具有从左到右的关联性

逻辑或(| |)

如果其中一个或两个操作数为真,逻辑OR运算符(| |)将返回布尔值true,否则返回false。操作数在求值之前隐式转换为bool类型,结果为bool类型。逻辑OR具有从左到右的关联性

因此,如果你有:

bool someVariable = true;
bool someOtherVariable = false;

if ((someVariable == true) && (someOtherVaribale == true))
{
    //This code will be executed
}

if ((someVaribale == true) || (someOtherVariable == true))
{
    //This code will be executed
}

您使用的是
|
(),如果任何操作数为true,则其计算结果将为true。因此,第一个条件(
iNumber!=8
)为false,而第二个条件(
iNumber!=9
)为true,因此总体结果为true

它与
&&
一起工作的原因是AND运算符要求两个操作数都为true,才能计算为true。如果其中一个操作数为false,则整个结果为false


您应该看到:

您使用的是
|
(),如果任何操作数为true,则其计算结果将为true。因此,第一个条件(
iNumber!=8
)为false,而第二个条件(
iNumber!=9
)为true,因此总体结果为true

它与
&&
一起工作的原因是AND运算符要求两个操作数都为true,才能计算为true。如果其中一个操作数为false,则整个结果为false


您应该看到:

此代码中的
if
条件将始终评估为
true

if (iNumber != 8 || iNumber != 9)
iNumber
8
时,它不等于
9
,因此第二部分为
true
。当
iNumber
9
时,它不等于
8
,因此第一部分为
true
。任何其他的,并且双方都是
正确的
<代码>|条件导致
true
,其中任意一侧为
true
。这是不可能的
false
。您希望在此处使用
&&
而不是
|

if (iNumber != 8 && iNumber != 9)
或者您可以使用并获取以下信息:

if (! (iNumber == 8 || iNumber == 9))

此代码中的
if
条件将始终评估为
true

if (iNumber != 8 || iNumber != 9)
iNumber
8
时,它不等于
9
,因此第二部分为
true
。当
iNumber
9
时,它不等于
8
,因此第一部分为
true
。任何其他的,并且双方都是
正确的
<代码>|条件导致
true
,其中任意一侧为
true
。这是不可能的
false
。您希望在此处使用
&&
而不是
|

if (iNumber != 8 && iNumber != 9)
或者您可以使用并获取以下信息:

if (! (iNumber == 8 || iNumber == 9))

它正在输入语句,因为该语句在计算iNumber时变为true!=九,

如果任何语句为true,if中的| |(或运算符)将为true

这样想吧

8 != 8 is False
8 != 9 is True

if ( False || True )
{
    //Do Stuff
}

它正在输入语句,因为该语句在计算iNumber时变为true!=九,

如果任何语句为true,if中的| |(或运算符)将为true

这样想吧

8 != 8 is False
8 != 9 is True

if ( False || True )
{
    //Do Stuff
}

| | OR运算符表示只有一个或另一个必须为真。在这种情况下,iNumber!=9,这样代码的一部分就为true,并输入语句。我想您应该使用&&和运算符来表示它不可能是8,也不可能是9。

OR运算符意味着只有一个或另一个必须为真。在这种情况下,iNumber!=9,这样代码的一部分就为true,并输入语句。我想您应该使用&&和运算符来表示它不能是8,也不能是9。

该语句在逻辑上等同于

    if (!(iNumber == 8 && iNumber == 9))
    {
        dPrice = dPrice + dTAX;
    }
这总是正确的,因为数字不能同时是8和9

你想要:

    if (iNumber != 8 && iNumber != 9)
    {
        dPrice = dPrice + dTAX;
    }


从逻辑上讲,无论哪个对您更有意义。

该语句在逻辑上等同于

    if (!(iNumber == 8 && iNumber == 9))
    {
        dPrice = dPrice + dTAX;
    }
这总是正确的,因为数字不能同时是8和9

你想要:

    if (iNumber != 8 && iNumber != 9)
    {
        dPrice = dPrice + dTAX;
    }


逻辑上,无论哪个对您更有意义。

在英语中,它的意思是:如果iNumber不是8或iNumber不是9。 iNumber是8,而不是9(您的第二个检查),因此它会被放入if块

你在描述一个和的条件,它不等于8,也不等于9

在英语中,这是一个