【问题标题】:For Loop Odd and Even Numbers [duplicate]For循环奇数和偶数[重复]
【发布时间】:2016-02-04 05:23:59
【问题描述】:

我正在使用以下逻辑/算法创建报告:

考虑以下几点:

  1. 输入一个计数n
  2. 打印 n
  3. 如果 (n==1) 则停止
  4. 否则,如果 n 为奇数,则 n = 3*n+1
  5. 否则 n = n/2
  6. 返回步骤 2

我有一个静态函数:

static int nCalc()
{
  int n;
  for(n=1; n<=200; n++)
  {

    if (n == 1)
    {
      fileOut.WriteLine("{0}", n);
    }
    else if (n % 2 == 0)
    {
      n = 3 * n + 1;
    }
    else
    {
      n = n / 2;
    }
    return n;

  }

}

我得到:

CS0161 - not all code paths return a value

这就是我迷路的地方。 else 和 else if 是否应该有一个返回布尔值?我可能没有正确的算法。

我将基本上做的是使用此算法循环从 1 到 200 的每个数字。所以我应该有 n 输出,例如:

fileOut.WriteLine(" N  RCL    N  RCL    N  RCL    N  RCL    N  RCL    N  RCL    N  RCL");
fileOut.WriteLine("--- ---   --- ---   --- ---   --- ---   --- ---   --- ---   --- ---");

在 N 下,我将有 1、2、3、4,然后在 RCL 下,在我的数字旁边,我将有 1、2、8、3 等。

谢谢,

【问题讨论】:

  • 如果你有一个返回,为什么还要有一个 for 循环?
  • for 循环之后需要一个return
  • 你应该把你的 if --- up to --- return n 放在一个函数中(n int){blah, blah}。然后在你的循环中,调用函数。
  • Duplicate 有非常详细的解释是什么可能导致循环出现此类错误,MSDN 解释了if - CS0161。如果该信息不足以找到答案,请提出新问题。

标签: c# algorithm for-loop


【解决方案1】:
static int nCalc()
{
  int n;
  for(n=1; n<=200; n++)
  {

    if (n == 1)
    {
      fileOut.WriteLine("{0}", n);
    }
    else if (n % 2 == 0)
    {
      n = 3 * n + 1;
    }
    else
    {
      n = n / 2;
    }


  }
 return n;
}

return 在循环之外

【讨论】:

    【解决方案2】:

    您要做什么尚不清楚,但要回答您的问题:

    static int nCalc()
    {
      for(...)
      {
        ...
        return n;
      }
      // If the code gets here there is no return.
      // The compiler doesn't know the code can't get here hence the error
    }
    

    【讨论】:

      【解决方案3】:

      return 语句移出for 循环:

      static int nCalc()
      {
      
          int n;
      
          for(n = 1; n <= 200; n++)
          {
              if (n == 1)
              {
                  fileOut.WriteLine("{0}", n);
              }
              else if (n % 2 == 0)
              {
                  n = 3 * n + 1;
              }
              else
              {
                  n = n / 2;
              }
          }
      
          return n;
      }
      

      正如最初所写,您在 for 循环完成执行之前退出了 if 块,因此出现了警告。

      【讨论】:

        【解决方案4】:

        我想你是故意的

        static void Main() 
        {
           for(int n=1;n<=200;n++)
           {
               fileOut.WriteLine("{0}",Calc(n));
           }
        }
        
        static int Calc(int n)
        {
            int result;
        
            if (n == 1)
            {
                result = 0;
            }
            else if (n % 2 == 0)
            {
              result = 3 * n + 1;
            }
            else
            {
              result = n / 2;
            }
        
            return result;
        }
        

        【讨论】:

          【解决方案5】:

          一个方法应该总是有一个可以在任何条件下执行的返回语句。在您的情况下,return 语句位于 for 循环中,如果不满足循环条件,则最终不会执行。

          将 return 语句移出循环将解决您的问题。

          static int nCalc()
          {  
             int n;  
             for(...)  
             {    
                ...  
             }  
             return n;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-11-11
            • 2012-10-10
            • 2021-10-08
            • 2012-02-17
            • 1970-01-01
            • 1970-01-01
            • 2011-09-28
            • 1970-01-01
            相关资源
            最近更新 更多