第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

錯(cuò)誤:'Program.Coefficient()':并非所有代碼路徑都返回一個(gè)值

錯(cuò)誤:'Program.Coefficient()':并非所有代碼路徑都返回一個(gè)值

C#
海綿寶寶撒 2022-12-31 14:02:57
我不希望該else語句返回一個(gè)值,而是再次運(yùn)行該方法。但是,我收到編譯時(shí)錯(cuò)誤'Program.Coefficient()':并非所有代碼路徑都返回一個(gè)值。我如何擺脫這個(gè)錯(cuò)誤?這是代碼: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貢獻(xiàn)1744條經(jīng)驗(yàn) 獲得超4個(gè)贊

當(dāng)您的函數(shù)返回值時(shí),這意味著您需要從每個(gè) 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


        }

我寧愿重構(gòu)您的代碼以最小化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).");

        }


    }


查看完整回答
反對(duì) 回復(fù) 2022-12-31
?
炎炎設(shè)計(jì)

TA貢獻(xiàn)1808條經(jīng)驗(yàn) 獲得超4個(gè)贊

該錯(cuò)誤意味著至少一種流可能性不返回值,這是您案例中的最后一個(gè)“其他”。

最后一行應(yīng)該是:

return Coefficient();


查看完整回答
反對(duì) 回復(fù) 2022-12-31
?
婷婷同學(xué)_

TA貢獻(xiàn)1844條經(jīng)驗(yàn) 獲得超8個(gè)贊

我建議重新設(shè)計(jì)例程(我看不到遞歸的任何需要)。您可以實(shí)現(xiàn)一個(gè)循環(huán),以便在用戶輸入 ( 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因?yàn)樗鼤?huì)在無效字符串上拋出異常"bla-bla-bla"(例如,如果用戶輸入);改用Double.TryParse。


查看完整回答
反對(duì) 回復(fù) 2022-12-31
  • 3 回答
  • 0 關(guān)注
  • 170 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)