【问题标题】:"not all code paths return a value" even when code returns for all inputs“并非所有代码路径都返回一个值”,即使所有输入的代码都返回
【发布时间】:2014-02-07 08:33:29
【问题描述】:

我正在尝试编写代码来返回给定整数是否可以被 1 到 20 整除,但我一直收到

错误 CS0161:“ProblemFive.isTwenty(int)”:并非所有代码路径都返回值”

这是我的代码:

public static bool isTwenty(int num)
{
    for(int j = 1; j <= 20; j++)
    {
        if(num % j != 0)
        {
            return false;
        }
        else if(num % j == 0 && num == 20)
        {
            return true;
        }
    }
}

【问题讨论】:

    标签: c#


    【解决方案1】:

    您看到该方法总是返回一些东西的事实并不意味着编译器可以看到它。

    编译器只做简单的检查,在你的例子中,逻辑太复杂了,编译器看不到。

    它甚至不够聪明,不能允许这种方法:

    public static bool isTwenty(int num)
    {
        for (int j = 1; j <= 20; j++)
        {
            return true;
        }
    }
    

    解决方案

    在方法末尾添加另一个return

    public static bool isTwenty(int num)
    {
        for(int j = 1; j <= 20; j++)
        {
            if(num % j != 0)
            {
                return false;
            }
            else if(num % j == 0 && num == 20)
            {
                return true;
            }
        }
    
        return true;
    }
    

    但在这种情况下,你可以让你的 for 循环有点不同,然后删除 else if 部分:

    public static bool isTwenty(int num)
    {
        for(int j = 1; j <= 20; j++)
        {
            if(num % j != 0)
            {
                return false;
            }
        }
    
        return true;
    }
    

    【讨论】:

    • 这些代码示例对isTwenty(0) 的响应是什么?
    • @MichaelBlackburn 这不是问题的重点。我不认为代码做OP想要的。我只是在展示如何摆脱错误。
    • @MarcinJarszek 够公平的。
    • +1。也可以使用while(true)for(...;true;...) 转换为众所周知的无限循环(也发布示例作为答案)。
    【解决方案2】:

    您有一个if 和一个else if,但没有else。不能保证ifelse if 中的任何一个都会被评估。

    【讨论】:

    • 不是if/else if,编译器甚至无法确定控制是否会进入for循环,无论条件如何。
    • 非常有趣。我看了看,并认为由于循环计数器不使用变量,编译器将能够确定是否完全进入了循环。如果不是这样,编译器如何展开循环?
    【解决方案3】:

    你还必须在循环之后从方法中返回一个值。

    实际上,您的else if 条件与您刚刚完成循环中的迭代一样,因此这样做更简单:

    public static bool isDivisibleByAllLessThanOrEqualToTwenty(int num) {
        for (int j = 1; j <= 20; j++) {
            if(num % j != 0) {
                return false;
            }
        }
        return true;
    }
    

    【讨论】:

      【解决方案4】:

      或者只是一点点单线(当然没用),以避免忘记返回

      public static bool isTwenty(int num)
      {
         return Enumerable.Range(1, 20).All(x => num % x == 0);
      }
      

      【讨论】:

        【解决方案5】:

        您需要考虑如果j 的所有迭代都不满足这两个条件会发生什么。

        编辑:确定一个数字是否能被固定范围内的所有数字整除的更有效方法是取范围的最小公倍数 (LCM),然后简单地检查所述数字可被 LCM 整除。

        public static bool isTwenty(int num)
        {
                         // compile-time computation
            const int lcm = 2 * 2 * 2 * 2 * 3 * 3 * 5 * 7 * 11 * 13 * 17 * 19;        
            return num % lcm == 0;
        }
        

        【讨论】:

          【解决方案6】:

          其他人已经在代码方面回答了为什么,但也许一个示例会有所帮助? 您的代码对以下输入的响应是什么?

          isTwenty(0);
          
          isTwenty(21);
          
          isTwenty(-1);
          

          【讨论】:

          • 这应该是一个答案吗?如果您对 OP 有任何疑问,请在 cmets 中提问。这就是 cmets 的好处;)
          • 是的,这是一个答案,问题是可能不满足一个或多个条件(在for 循环或if 条件中)。 “如果您对海报有任何疑问,请在 cmets 中提问”,您也不必投反对票。
          • 这是一个答案?如果这是一个答案,那么什么是问题?
          【解决方案7】:

          如果您想保留您的逻辑,另一种解决方案 - 将 for 替换为众所周知的无限循环,因为 知道它总是会退出。如果:

          public static bool isTwenty(int num)
          {
              for(int j = 1; true; j++)
              {
                  if(num % j != 0)
                  {
                      return false;
                  }
                  else if(num % j == 0 && num == 20)
                  {
                      return true;
                  }
              }
          }
          

          或者如果while 效果更好:

              var j = 1;
              while(true) 
              {
                  if (...){...}
                  j++;
          
              }
          

          【讨论】:

            猜你喜欢
            • 2011-12-17
            • 1970-01-01
            • 1970-01-01
            • 2017-07-14
            • 1970-01-01
            • 1970-01-01
            • 2021-11-08
            相关资源
            最近更新 更多