【问题标题】:I need help understanding bool = false and the meaning of -1我需要帮助理解 bool = false 和 -1 的含义
【发布时间】:2020-03-26 07:21:29
【问题描述】:

这里是编程新手。

我在此项目中有一项要求,即检查输入的重复球员号码(例如 Michael Jordan 23)。不能让 Dave Brown 23,因为 Michael 已经分配了那个号码。

我需要帮助来理解这段代码中发生了什么。

这是我下面的内容。我评论了我认为正在发生的事情。

    static int SearchNumber(int[] playerNumbers, int newPlayerNumber, int playerCount)
    {
        //3. //

        int index = 0;
        bool found = false; //found duplicate number = not true

        while (!found && index < playerCount)//while we did find a duplicate number && index < playerCount
        {
            if (playerNumbers[index] == newPlayerNumber)
            {
                found = true; // we found a duplicate number
            }
            else //we did not find a duplicate number
            {
                index++; //add onto the team
            }

        }
        if (!found) //if we did find a duplicate number
        {
            index = -1; //subtract 1 from the team. return to menu
        }
        return index;
    }

下面是我不明白的部分:

Screen shot of code with highlighted to understand what I don't understand

谢谢。

【问题讨论】:

  • -1 是一些程序用来指定“我们没有找到索引”同时保证返回值是整数的约定。
  • 旁注:SearchNumber 对该方法应该做什么的描述非常糟糕。投资于能够准确描述其功能的命名方法,您将花费更少的时间找出代码。
  • 设置bool found = false 是在说“我假设我们没有找到它,但如果我找到它,我将设置found = true。”
  • @JJLe 该网站仅在您接受答案并为您喜欢的答案投票时才有用。让问题悬而未决无济于事

标签: c#


【解决方案1】:

从评论中删除了此信息,但我认为这个问题本身不足以让我放心地将我的名字放在上面。


返回-1 是一些程序用来指定“我们没有找到索引”同时保证返回值为整数的约定。

例如,IndexOf 将始终返回一个整数 - 找到匹配时返回正索引,否则返回 -1。

我只能推测为什么,但根据我的经验,我见过较旧的程序和 API 使用这种方法,而较新的程序和 API 要么抛出异常,要么返回一个 可为空的 em> 整数。

【讨论】:

    【解决方案2】:

    这应该是此代码的正确 cmets

    static int SearchNumber(int[] playerNumbers, int newPlayerNumber, int playerCount)
    {
        int index = 0; // iteration index
        bool found = false; // flag raised when player found
    
        while (!found && index < playerCount)//while we did find a duplicate number && index < playerCount
        {
            if (playerNumbers[index] == newPlayerNumber) // checks if currently iterated player has the searche number
            {
                found = true; // raises the flag - player found
            }
            else //we did not find a duplicate number
            {
                index++; // simply increase iterator index
            }
    
         }
         if (!found) //  if we did !!! NOT !!! find a duplicate number
         {
            index = -1; // SET return value to -1 to indicate nothing found
         }
         return index;
    }
    

    现在和 .NET 优化的方法相同

    static int SearchNumber(int[] playerNumbers, int newPlayerNumber)
    {
        int len = playerNumbers.Length;
        for (int i = 0; i < len; i++)
        {
            if (playerNumbers[i] == newPlayerNumber)  
                return i;
        }
        return -1;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-10-07
      • 2019-11-03
      • 1970-01-01
      • 2022-11-28
      • 2012-03-31
      • 2012-06-24
      • 1970-01-01
      • 1970-01-01
      • 2013-04-18
      相关资源
      最近更新 更多