简体   繁体   English

模量Visual Studio 2013

[英]Modulus Visual Studio 2013

I was just wondering how I could state if x + y + z % 2 == 0 BUT only do this statement IF the three values aren't set to 0. I require the values to be 0. 我只是想知道如何声明x + y + z%2 == 0,但是如果三个值未设置为0,则仅执行此语句。我要求将值设置为0。

Example of my code & posStatus may hold variables of the value 0. The problem is when all three of them are 0 an issue occurs. 我的代码和posStatus的示例可能包含值为0的变量。问题是,当三个变量都为0时,就会发生问题。 The user and program over time fill in these values. 用户和程序随时间填充这些值。

else if (posStatus[0] + posStatus[4] + posStatus[8] % 2 == 0)
{
    if (posStatus[0] == 0)
    {
        posStatus[0] = 2;
        return;
    }
    else if (posStatus[4] == 0)
    {
        posStatus[4] = 2;
        return;
    }
    else if (posStatus[8] == 0)
    {
        posStatus[8] = 2;
        return;
    } 
}
if((x!=0 && y!=0 && z!=0) && x + y + z % 2 == 0)
    //do your stuff

Logical operators in C# are converted to series of if-else constructions during compilation, so if first condition is not true, other ones won't be checked. C#中的逻辑运算符在编译期间会转换为一系列if-else构造,因此,如果第一个条件不为真,则不会检查其他条件。

if(posStatus[0] != 0 &&
   posStatus[4] != 0 &&
   posStatus[8] != 0 &&
   (posStatus[0] + posStatus[4] + posStatus[8]) % 2 == 0)
{
    // Do something
}
if((posStatus[0]!=0 && posStatus[4]!=0 && posStatus[8]!=0) && 
   (posStatus[0] + posStatus[4] + posStatus[8]) % 2 == 0)
{

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM