-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结构,并决定:这个条件块的末尾是我返回的地方,还是在此之后逻辑会继续?逐步了解每一种可能性,并亲自查看您是否在正确的区域着陆。

您可以通过良好的格式帮助自己成为更好的编码员。例如,在“-500 到 500 之间的 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;  

这很糟糕,原因有很多,其中最起码的原因是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 回答