简体   繁体   English

C#-基本的if语句查询-找不到答案

[英]C# - basic if statement inquiry - can't find the answer

this has probably been answered but i don't know how to frame the question, therefore can't find a past answer. 这可能已经被回答了,但是我不知道如何去构想这个问题,因此找不到过去的答案。

so what i wanna do is something like this: (more variables for the same if check) 所以我想做的是这样的:(如果检查相同,会有更多变量)

if ((aa && bb && cc) == 1)
    {
       win = 1;
    }

what's the correct syntax for this? 正确的语法是什么?

thanks a bunch 谢谢一大堆

如果您有很多变量,则还可以构建一个值数组并使用IEnumerable.All方法来验证它们是否都是1. IMO,更具可读性。

if (new[] { aa, bb, cc}.All(x => x == 1))
if (aa == 1 && bb == 1 && cc == 1)
{
   win = 1;
}

You could check each variable individually, such as the following: 您可以单独检查每个变量,例如:

if (aa == 1 && bb == 1 && cc == 1)
{
    // do something
}

If you know that you'll always be checking against the value of 1, you may want to declare it as a constant. 如果您知道将始终对照值1进行检查,则可能需要将其声明为常量。

public const int ExpectedValue= 1;
if (aa == ExpectedValue&& bb == ExpectedValue&& cc == ExpectedValue)
{
    // do something
}

I am afraid you will have to do it the long way, There is no simple short clean way of checking if multiple vars have the same value in an if statement. 恐怕您将不得不做很长的路要走,没有简单的简洁方法可以检查if语句中多个var是否具有相同的值。

Code: 码:

If (aa == 1 && bb == 1 && cc == 1)
{
    // Code here.
}

Hope this helps. 希望这可以帮助。

The data type of the variable does make a difference as to what you could do with the if statement. 变量的数据类型确实对使用if语句可以执行的操作有所不同。


Boolean 布尔

If they are boolean values and you want to know when they are one, you can go: 如果它们是boolean值,并且您想知道它们何时为1,则可以执行以下操作:

if(aa && bb && cc) { }

Because each variable is either one or zero, if one of them were false then the whole statement would be false. 因为每个变量都是一个或零,所以如果其中一个为假,则整个语句为假。 Only, when all are one will this statement be true. 只有当全部为1时,此陈述才为真。


Integer 整数

If you want to have the look of 如果你想看看

if((aa && bb && cc) == Value) {}

You can multiply them and see if they are equal to the cube of Value . 您可以将它们相乘,看看它们是否等于Value的立方。

if(aa*bb*cc == Value*Value*Value) {}

So, if Value equals one then you get: 因此,如果Value等于1,那么您将得到:

if(aa*bb*cc == 1) {}

Best Way 最好的办法

The best way to do this would be to individually check that each variable is equal to Value . 最好的方法是单独检查每个变量是否等于Value

if(aa == Value && bb == Value && cc == Value) {}

This is because the && operator expects two bool s then returns a boolean value. 这是因为&&运算符期望两个bool然后返回一个boolean值。 So, when you go aa && bb && cc you are literally "anding" these values together to produce a boolean value. 因此,当您使用aa && bb && cc时,实际上是将这些值“相加”在一起以产生boolean值。 By first comparing each variable to the Value you are creating a boolean value for each one then "anding" that together to produce a similar result as the first solution I presented. 通过首先将每个变量与Value进行比较,您将为每个变量创建一个boolean值,然后将它们“与”在一起,以产生与我提出的第一个解决方案相似的结果。


Hope this helps! 希望这可以帮助!

You can use this code , and its very simple and easy 您可以使用此代码,它非常简单容易

if ( aa == 1 && bb == 2 && cc == 3)
{
    // your codes
}

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

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