-7

これが以前に尋ねられていないことを願っています。return ステートメントがメソッド内にあるすべてのリターン パスを強調表示する方法はありますか? 原因は、このかなり長いコード ブロックに多くのパスがあり、まだすべてのパスを見つけられていないからです。(便利なツールだと思います)乾杯。

 static double findconversion(int menuOption, int submenuOption) {

        if (menuOption == 1) {

            if (submenuOption == 1) {

                    Console.Write("\nYou chose to convert Celcius to Fahrenheit" +            "\nEnter the number that you want to convert, (between -500 and 500)"
                        + "\nOr enter 0 to return to the previous menu: ");
                    double celnum = int.Parse(Console.ReadLine()); 


                if (celnum == 0) {
                    Console.WriteLine("\nYou cancelled your selection"); return celnum;

                } else if ((-500 > celnum) || (celnum > 500)) {
                    Console.WriteLine("\nchoose a number between -500 to 500 please"); 

                    findconversion(menuOption, submenuOption); return celnum;

                } else if ((-500 <= celnum) && (celnum <= 500)) {

                    double result = Celsiusandfahrenheit(celnum, submenuOption);

                    if (submenuOption == 1) {
                        Console.WriteLine("\n   " + celnum + (" degrees celcius converted to fahrenheit is: {0:0.00} degrees fahrenheit"), result);
                        return celnum;
                    } else if (submenuOption == 2) {
                        Console.WriteLine("\n   " + celnum + (" degrees fahrenheit converted to celcius is: {0:0.00} degrees celcius"), result);
                        return celnum;
                    } return celnum;
                }

            } else if (submenuOption == 2) {
                Console.Write("\nYou chose to convert Fahrenheit to Celsius" + "\nEnter the number that you want to convert, (between -500 and 500)"
                    + "\nOr enter 0 to return to the previous menu: "); 


                double celnum = int.Parse(Console.ReadLine()); 




                if (celnum == 0) {
                    Console.WriteLine("\nYou cancelled your selection"); return celnum;

                } else if ((-500 > celnum) || (celnum > 500)) {
                    Console.WriteLine("\nchoose a number between -500 to 500 please");


                    findconversion(menuOption, submenuOption); return celnum;
                } else if ((-500 <= celnum) && (celnum <= 500)) {

                    double result = Celsiusandfahrenheit(celnum, submenuOption);
                    if (submenuOption == 2) {
                        Console.WriteLine("\n   " + celnum + (" degrees fahrenheit converted to celcius is: {0:0.00} degrees celcius"), result); return celnum;
                    }




                } return celnum;


            } 

        } else if (menuOption == 2) {

            if (submenuOption == 1) {
                Console.Write("\nYou chose to convert centimetres to feet and inches" + "\nEnter the number that you want to convert to feet and inches, (between -500 and 500)."
                    + "\nOr enter 0 to return to the previous menu: ");
                double celnum = int.Parse(Console.ReadLine()); 

                if (celnum == 0) {
                    Console.WriteLine("\nYou cancelled your selection"); return celnum;

                } else if ((-500 > celnum) || (celnum > 500)) {
                    Console.WriteLine("\nchoose a number between -500 to 500 please"); 

                    findconversion(menuOption, submenuOption); return celnum;

                } else if ((-500 <= celnum) && (celnum <= 500)) {


                    double result = cmsandfeet(celnum, submenuOption);
                    // double result1 = cmsandinches(centnum, submenuOption, empty);
                    result = Math.Floor(result);
                    double result1 = ((celnum - (result * 30.48)) / 2.54);
                    if (submenuOption == 1) { Console.WriteLine("\n   " + celnum + (" centimetres converted to feet and inches is: {0:0} feet and {1:0.00} inches"), result, result1); } return celnum;


                } 

                return celnum;
            } 

        }
    }
4

1 に答える 1

1

私はコメントとしてこれに答え始めましたが、完全な答えを保証するのに十分な有益な内容があるかもしれないことに気付きました.

開発者は、ロジックの流れを把握し、コード内のすべてのパスがどこにあるかを把握する責任があります。非常に一般的な経験則として、すべてのif/else構造を注意深く調べて、次のように判断します。この条件付きブロックの最後は、私が戻る場所であるか、それともロジックはこの後も続くか? それぞれの可能性を確認し、正しい領域に着地しているかどうかを自分で確認してください。

適切にフォーマットすることで、より優れたコーダーになることができます。たとえば、「celnum between -500 and 500」ブロックの最後には、次のようになります。

if (submenuOption == 1) { Console.WriteLine("\n   " + celnum + (" centimetres converted to feet and inches is: {0:0} feet and {1:0.00} inches"), result, result1); } return celnum;  

それは多くの理由で貧弱ですが、その中で最も重要なのreturn celnumは、本当に長い行の最後にあることです。それは条件に関係なく実行されますが、よく調べないとわかりません。代わりに、これははるかに明確です。

if (submenuOption == 1) { 
    Console.WriteLine("\n   " + celnum + (" centimetres converted to feet and inches is: {0:0} feet and {1:0.00} inches"), result, result1);
}
return celnum;

これは、submenuOption の値に関係なく、値を返していることを視覚的に直接示しています。

他に確認する必要があるのは、デフォルトのケース、つまり「暗黙のelse」ケースです。いくつかのケースでこれを行いますが、これは良いことです (たとえば、return celnum「-500 から 500 の範囲内にありますか?」の後で)。エラーの原因となっているそれを行わない場所は、最も外側の条件です。

if submenuOption == 1ありますがelse if submenuOption == 2、その後は何もありません。submenuOption が 1 または 2 でない場合はどうなりますか? その場合、何も返しません。既知のケース以外では、デフォルトのケースが必要です。エラーをスローするか、適切なオプションが選択されなかったことを示す値を返します。これでコンパイラの問題が修正されるはずです。

于 2013-04-17T14:30:01.383 に答える