CS0019 C# 运算符“||”不能应用于 'int' 和 'int' 类型的操作数
CS0019 C# Operator '||' cannot be applied to operands of type 'int' and 'int'
我有一个带整数的或运算符 (||),但由于某种原因它给了我一个错误。整数是位置。我想跟踪玩家的位置,这样我就可以在一段代码中控制所有的动作。看起来像 if (location == (1 || 2 || 3)
的行给我错误。
Actions:
Console.WriteLine("");
Console.Write("What should i do? ");
string move = Console.ReadLine();
if (move.Contains("north"))
{
if (location == (1 || 2 || 3))
{
Console.WriteLine("There is a cliff here I cant climb up!");
}
}
else if (move.Contains("east"))
{
if (location == (3 || 6 || 9))
{
Console.WriteLine("There is a cliff here I cant climb up!");
}
}
else if (move.Contains("south"))
{
if (location == (7 || 8 || 9))
{
Console.WriteLine("There is a cliff here I cant climb up!");
}
}
else if (move.Contains("west"))
{
if (location == (1 || 2 || 3))
{
Console.WriteLine("There is a cliff here I cant climb up!");
}
}
else if (move.Contains("grab"))
{
if (location == (6))
{
Console.WriteLine("I don't have time for apples!");
}
else if (location == (8))
{
Console.WriteLine("It's locked!");
}
else if (location == (1 ||2 || 4 || 5 || 7 || 9))
{
Console.WriteLine("There is nothing to grab!");
}
else if (location == (3))
{
Console.WriteLine("I grabbed the key.");
bool key = true;
}
}
else if (move.Contains("use"))
{
}
感谢您的帮助。
应该是这样的:
if(location == 1 || location == 2 || location == 3)
你不能这样写条件,而是使用:
if (location == 7 || location == 8 || location == 9 )
或者如果您愿意,也可以使用 switch
语句。
在 C# 中,您不能将一个变量与多个值进行比较。你的代码应该是这样的:
if (location == 1 || location == 2 || location == 3) { /* some code */ }
但是因为你使用的是特定范围内的数字,所以用起来比较短
if (location > 0 && location < 4) { /* some code */ }
你也可以写一个函数来比较多个值:
bool Compare(object variable, params object[] values) {
foreach (object value in values) if (!(variable == value)) return false;
return true;
}
我的印象是您应该更加熟悉编程语言与自然语言不同这一事实。
用自然语言你会说 "if that number is 1 or 2 or 3, do something" 并且每个人都会理解你。
另一方面,编程语言大多建立在逻辑和算术之上。如果机器(编译器)可以对您编写的内容应用一组有限的规则,那么它更容易理解您想要什么。
如果它有更多的规则,它 可能 给你额外的选择来写 "location == (1 || 2 || 3)" 并将它翻译成其他答案告诉你写的内容反而。
这种方法的缺点是,这个额外的选项很可能会引入你最终可能会称之为模棱两可或矛盾的东西,你写了其他东西,编译器会把它翻译成你永远不会理解的东西预期的。
这个案例的规则基本很明确:
- 1、2、3是表达式(很琐碎的)
- 在x||y||是一个运算符,它需要两个 expressions/operands(一个在左边,x,一个在右边,y)来形成另一个表达式(可以说是递归的)
- ||期望其操作数 评估(即:计算)为 true 或 false(像 1,2,3 这样的数字并不适合那里,或者你会说 3 是 true 还是 false ; 旁注:一些编程语言甚至会在预期为真时接受数字)并且也会评估为 true 或 false
- x==y 是一个运算符,它接受两个表达式(可以是数字或真值),最终计算结果为 true 或 false
- if (x) y 是一个语句,它接受括号中的真值表达式,以便执行括号后面的内容。
总结一下:编译错误表明您还没有理解编译器如何解释语言结构。
我有一个带整数的或运算符 (||),但由于某种原因它给了我一个错误。整数是位置。我想跟踪玩家的位置,这样我就可以在一段代码中控制所有的动作。看起来像 if (location == (1 || 2 || 3)
的行给我错误。
Actions:
Console.WriteLine("");
Console.Write("What should i do? ");
string move = Console.ReadLine();
if (move.Contains("north"))
{
if (location == (1 || 2 || 3))
{
Console.WriteLine("There is a cliff here I cant climb up!");
}
}
else if (move.Contains("east"))
{
if (location == (3 || 6 || 9))
{
Console.WriteLine("There is a cliff here I cant climb up!");
}
}
else if (move.Contains("south"))
{
if (location == (7 || 8 || 9))
{
Console.WriteLine("There is a cliff here I cant climb up!");
}
}
else if (move.Contains("west"))
{
if (location == (1 || 2 || 3))
{
Console.WriteLine("There is a cliff here I cant climb up!");
}
}
else if (move.Contains("grab"))
{
if (location == (6))
{
Console.WriteLine("I don't have time for apples!");
}
else if (location == (8))
{
Console.WriteLine("It's locked!");
}
else if (location == (1 ||2 || 4 || 5 || 7 || 9))
{
Console.WriteLine("There is nothing to grab!");
}
else if (location == (3))
{
Console.WriteLine("I grabbed the key.");
bool key = true;
}
}
else if (move.Contains("use"))
{
}
感谢您的帮助。
应该是这样的:
if(location == 1 || location == 2 || location == 3)
你不能这样写条件,而是使用:
if (location == 7 || location == 8 || location == 9 )
或者如果您愿意,也可以使用 switch
语句。
在 C# 中,您不能将一个变量与多个值进行比较。你的代码应该是这样的:
if (location == 1 || location == 2 || location == 3) { /* some code */ }
但是因为你使用的是特定范围内的数字,所以用起来比较短
if (location > 0 && location < 4) { /* some code */ }
你也可以写一个函数来比较多个值:
bool Compare(object variable, params object[] values) {
foreach (object value in values) if (!(variable == value)) return false;
return true;
}
我的印象是您应该更加熟悉编程语言与自然语言不同这一事实。
用自然语言你会说 "if that number is 1 or 2 or 3, do something" 并且每个人都会理解你。
另一方面,编程语言大多建立在逻辑和算术之上。如果机器(编译器)可以对您编写的内容应用一组有限的规则,那么它更容易理解您想要什么。
如果它有更多的规则,它 可能 给你额外的选择来写 "location == (1 || 2 || 3)" 并将它翻译成其他答案告诉你写的内容反而。
这种方法的缺点是,这个额外的选项很可能会引入你最终可能会称之为模棱两可或矛盾的东西,你写了其他东西,编译器会把它翻译成你永远不会理解的东西预期的。
这个案例的规则基本很明确:
- 1、2、3是表达式(很琐碎的)
- 在x||y||是一个运算符,它需要两个 expressions/operands(一个在左边,x,一个在右边,y)来形成另一个表达式(可以说是递归的)
- ||期望其操作数 评估(即:计算)为 true 或 false(像 1,2,3 这样的数字并不适合那里,或者你会说 3 是 true 还是 false ; 旁注:一些编程语言甚至会在预期为真时接受数字)并且也会评估为 true 或 false
- x==y 是一个运算符,它接受两个表达式(可以是数字或真值),最终计算结果为 true 或 false
- if (x) y 是一个语句,它接受括号中的真值表达式,以便执行括号后面的内容。
总结一下:编译错误表明您还没有理解编译器如何解释语言结构。