【问题标题】:CS0019 C# Operator '||' cannot be applied to operands of type 'int' and 'int'CS0019 C# 运算符 '||'不能应用于“int”和“int”类型的操作数
【发布时间】:2018-03-03 12:46:59
【问题描述】:

我有一个带整数的或运算符 (||),但由于某种原因,它给了我一个错误。整数是位置。我想跟踪玩家的位置,这样我就可以在一段代码中控制所有的动作。看起来像 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 == (6)) 可以变成if (location == 6)
  • 请务必在发布问题之前查看语言文档。这是一个微不足道的 sintax 错误

标签: c# operators


【解决方案1】:

应该是这样的:

if(location == 1 || location ==  2 || location ==  3)

【讨论】:

    【解决方案2】:

    我的印象是,您应该多熟悉一下编程语言与自然语言这一事实​​。

    用自然语言你会说“如果那个数字是 1 或 2 或 3,做点什么”,每个人都会理解你。

    另一方面,编程语言大多建立在逻辑和算术之上。如果机器(编译器)可以将一组有限的规则应用于您编写的内容,那么它更容易理解您想要的内容。

    如果它有更多规则,它可能为您提供额外的选项来编写“location == (1 || 2 || 3)”并将其转换为其他答案的内容告诉你改写。

    这种方法的缺点是,这个附加选项很可能会引入您最终可能会称之为模棱两可或矛盾的东西,您在其中编写其他内容,编译器会将其翻译成您从未预料到的内容。

    这个案例的规则基本很清楚:

    • 1、2、3 是表达式(非常琐碎的)
    • 在 x||y 中 ||是一个运算符,它采用两个表达式/操作数(一个在左侧,x,一个在右侧,y)形成另一个表达式(可以说是递归)
    • ||期望它的操作数评估(即:计算)为真或假(像 1,2,3 这样的数字并不真正适合那里,或者你会说 3 是真还是假;旁注:一些编程语言甚至会接受期望为真的数字)并且也会评估为真或假
    • x==y 是一个运算符,它接受两个表达式(可以是数字或真值),最终计算结果为真或假
    • if (x) y 是一个在括号中接受此真值表达式的语句,以便执行或不执行括号后面的内容。

    总结一下:编译错误说明你还没有理解编译器是如何解释语言结构的。

    【讨论】:

      【解决方案3】:

      你不能这样写条件,而是使用:

      if (location == 7 || location == 8 || location == 9 )
      

      如果您愿意,也可以改用switch 语句。

      【讨论】:

        【解决方案4】:

        在 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;
        }
        

        【讨论】:

          猜你喜欢
          • 2011-04-29
          • 2017-09-21
          • 1970-01-01
          • 2017-09-02
          • 2018-08-13
          • 1970-01-01
          • 2015-01-25
          • 2011-12-31
          • 2022-01-07
          相关资源
          最近更新 更多