我希望之前没有问过这个问题,有没有办法突出显示返回路径应该放在方法中的所有返回路径?因为我有这个相当长的代码块与may路径,我仍然没有找到它的每个路径。 (我认为这将是一个有用的工具)欢呼。
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;
}
}
}
答案 0 :(得分: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;
这直接在视觉上向我显示您正在返回一个值,而不管子菜单选项的值。
您必须考虑的另一件事是拥有默认案例或“隐式else
”。你可以在几种情况下做到这一点,这很好,(例如,在“它在-500和500”之间的return celnum
)。如果不这样做,导致错误的是你最外面的条件。
您有if submenuOption == 1
,而且您有else if submenuOption == 2
,但此后您没有任何内容。如果submenuOption不是1或2会发生什么?在这种情况下,你不会返回任何东西。在已知情况之外,您需要一个默认情况 - 抛出错误,或返回一个值,表明没有选择好的选项。这应该可以解决您的编译问题。