为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
海绵宝宝撒 2022-12-31 14:02:57
我不希望该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();    }}
查看完整描述

3 回答

?
慕无忌1623718

TA贡献1744条经验 获得超4个赞

当您的函数返回值时,这意味着您需要从每个 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).");

        }


    }


查看完整回答
反对 回复 2022-12-31
?
炎炎设计

TA贡献1808条经验 获得超4个赞

该错误意味着至少一种流可能性不返回值,这是您案例中的最后一个“其他”。

最后一行应该是:

return Coefficient();


查看完整回答
反对 回复 2022-12-31
?
婷婷同学_

TA贡献1844条经验 获得超8个赞

我建议重新设计例程(我看不到递归的任何需要)。您可以实现一个循环,以便在用户输入 ( 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。


查看完整回答
反对 回复 2022-12-31
  • 3 回答
  • 0 关注
  • 100 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信