首页 > 解决方案 > OR with integers

问题描述

Is there a way to use an if statement to check if an integer is equal to another integer(s)? The code below gives an error saying that the || OR operator can't be used for types int and int. Is there a better way to work around this than making each option a separate IF statement?

if (role = 7 || 11)
{
    Console.WriteLine("You won!");
}

if (role = 2 || 3 || 12)
{
    Console.WriteLine("You lost.");
}

标签: c#if-statementoperators

解决方案


If statements require you to evaluate boolean values. So you need to change the second half of your if statement to evaluate to a boolean value:

if (role == 7 || role == 11)

Note that I changed = to == because = is an assignment, whereas == is a comparison.


推荐阅读