【问题标题】:Error: 'Program.Coefficient()': not all code paths return a value [duplicate]错误:'Program.Coefficient()':并非所有代码路径都返回值[重复]
【发布时间】:2019-09-15 22:09:44
【问题描述】:

我不希望else 语句返回一个值,而只是再次运行该方法。但是,我得到 编译时错误

'Program.Coefficient()':并非所有代码路径都返回值。

如何摆脱这个错误?

这是代码:

public static double Coefficient()
{
    string string1 = Console.ReadLine();
    string[] stringArray = string1.Split('^');

    double[] doubleArray = new double[stringArray.Length];

    for (int i = 0; i < stringArray.Length; i++)
    {
        doubleArray[i] = Double.Parse(stringArray[i]);
    }

    if (doubleArray.Length == 2)
    {
        double coefficient = Math.Pow(doubleArray[0], doubleArray[1]);
        return coefficient;
    }

    else if (doubleArray.Length == 1)
    {
        double coefficient = doubleArray[0];
        return coefficient;
    }
    else
    {
        Console.WriteLine("Please follow the specified input form (a^b).");
        Console.ReadKey();
        Coefficient();
    }
}

【问题讨论】:

    标签: c#


    【解决方案1】:

    我建议重新设计例程(我在递归中看不到任何需要)。您可以实现一个循环,以便在用户输入 (Console.ReadLine()) 有效值之前一直询问:

    public static double Coefficient() {
      while (true) {
        string input = Console.ReadLine();
    
        string[] items = input.Split('^');
    
        if (items.Length == 1) {
          if (double.TryParse(items[0], out double A))
            return A; // One valid value 
        }
        else if (items.Length == 2) {
          if (double.TryParse(items[0], out double A) && 
              double.TryParse(items[1], out double B))
            return Math.Pow(A, B); // Two valid values
        } 
    
        // Neither one valid value, nor two valid values pattern 
        Console.WriteLine("Please follow the specified input form (a^b)."); 
        // No need in "Console.ReadKey();" - the routine will stop on Console.ReadLine()
      }          
    } 
    

    小心Double.Parse,因为它在无效字符串上抛出异常(例如,如果用户输入"bla-bla-bla");请改用Double.TryParse

    【讨论】:

      【解决方案2】:

      当你的函数返回值时,这意味着你需要从每个 if..else 块到return double value

      这里你没有从 else 块返回任何值。您需要从 else 块返回双精度值

              else
              {
                  Console.WriteLine("Please follow the specified input form (a^b).");
                  Console.ReadKey();
                  return Coefficient();  // This will call recursively  same function. for recursion use return Coefficient() ;
                   //return 0; //If you don't want recursion, then comment above line and return 0
      
              }
      

      我希望重构您的代码以最小化 Coefficient() 方法中的代码。像,

         public static double Coefficient()
          {
              while (true)
              {
                  string string1 = Console.ReadLine();
                  string[] stringArray = string1.Split('^');
                  double[] doubleArray = Array.ConvertAll(stringArray, double.Parse);
      
      
                  if (doubleArray.Length == 2)
                  {
                      double coefficient = Math.Pow(doubleArray[0], doubleArray[1]);
                      return coefficient;
                  }
      
                  else if (doubleArray.Length == 1)
                  {
                      return doubleArray[0];
                  }
                Console.WriteLine("Please follow the specified input form (a^b).");
              }
      
          }
      

      【讨论】:

      • 我不希望'else'返回一个值,我只希望它再次运行该方法,直到通过'if'或'else if'语句返回一个值。跨度>
      • 更新了我的答案,请检查
      【解决方案3】:

      该错误意味着至少一个流可能性没有返回值,在您的情况下这是最后一个“else”。

      最后一行应该是:

      return Coefficient();
      

      【讨论】:

      • 这会再次运行该方法吗?
      • 足够时间后递归调用和stackoverflow异常?
      • @Anders:是的,并返回该运行的值(这是一个递归调用)。
      • @Metheny 我是编程新手,很抱歉有很多问题,但是当没有什么可以返回时,它如何返回运行值?我只是希望它再次运行该方法,直到通过“if”或“else if”语句返回一个值。
      • 原来的方法执行会调用同一个方法(这叫递归调用)。在第二次(内部,递归)执行中,如果用户输入不同的值,则条件将允许将“if”评估为“true”。并且返回值将返回给原始方法执行,该方法又将返回该值。
      猜你喜欢
      • 2014-06-05
      • 2013-07-01
      • 2014-04-16
      • 2015-02-07
      • 2012-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多