错误:'Program.Coefficient()':并非所有代码路径都返回一个值

我不希望该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();

    }

}


海绵宝宝撒
浏览 148回答 3
3回答

慕无忌1623718

当您的函数返回值时,这意味着您需要从每个 if..else 块中返回值return double value。在这里,您没有从 else 块返回任何值。您需要从 else 块返回双精度值&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Please follow the specified input form (a^b).");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.ReadKey();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Coefficient();&nbsp; // This will call recursively&nbsp; same function. for recursion use return Coefficient() ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//return 0; //If you don't want recursion, then comment above line and return 0&nbsp; &nbsp; &nbsp; &nbsp; }我宁愿重构您的代码以最小化Coefficient()方法中存在的代码。就像是 ,&nbsp; &nbsp;public static double Coefficient()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; while (true)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string string1 = Console.ReadLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string[] stringArray = string1.Split('^');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double[] doubleArray = Array.ConvertAll(stringArray, double.Parse);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (doubleArray.Length == 2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double coefficient = Math.Pow(doubleArray[0], doubleArray[1]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return coefficient;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (doubleArray.Length == 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return doubleArray[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Please follow the specified input form (a^b).");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

炎炎设计

该错误意味着至少一种流可能性不返回值,这是您案例中的最后一个“其他”。最后一行应该是:return&nbsp;Coefficient();

婷婷同学_

我建议重新设计例程(我看不到递归的任何需要)。您可以实现一个循环,以便在用户输入 ( Console.ReadLine()) 有效值之前一直询问:public static double Coefficient() {&nbsp; while (true) {&nbsp; &nbsp; string input = Console.ReadLine();&nbsp; &nbsp; string[] items = input.Split('^');&nbsp; &nbsp; if (items.Length == 1) {&nbsp; &nbsp; &nbsp; if (double.TryParse(items[0], out double A))&nbsp; &nbsp; &nbsp; &nbsp; return A; // One valid value&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; else if (items.Length == 2) {&nbsp; &nbsp; &nbsp; if (double.TryParse(items[0], out double A) &&&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double.TryParse(items[1], out double B))&nbsp; &nbsp; &nbsp; &nbsp; return Math.Pow(A, B); // Two valid values&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; // Neither one valid value, nor two valid values pattern&nbsp;&nbsp; &nbsp; Console.WriteLine("Please follow the specified input form (a^b).");&nbsp;&nbsp; &nbsp; // No need in "Console.ReadKey();" - the routine will stop on Console.ReadLine()&nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}&nbsp;小心,Double.Parse因为它会在无效字符串上抛出异常"bla-bla-bla"(例如,如果用户输入);改用Double.TryParse。
打开App,查看更多内容
随时随地看视频慕课网APP