c# - 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.");
}
解决方案
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.
推荐阅读
- cordova - Admob 和地理位置组合不起作用
- c# - DataGridViewComboBoxCell 默认显示行值
- apache - htaccess 阻止在 chrome 中使用全局 SSL 重定向
- html - 关闭选择下拉菜单时调整内容文本的大小/换行
- css - Vue-js-modal 可调整大小问题
- angular - mat-select required 无法重置
- spring - Spring Data Rest 暴露了非预期的 HTTP 方法
- cloudera - 在 cdsw 中获取状态作业
- sql-server - 如果您将表连接到链中 VS 连接到中央表,它会对性能产生影响吗?
- awk - grep 模式与行号匹配