I am attempting to write code (in visual studio 2012 using c#) that will allow a user to select an account using 1,2,3 or 4. i am using a do-while loop so i can use 0 to exit (break?) or run the loop again until a valid option is selected.
我正在尝试编写代码(在visual studio 2012中使用c#),允许用户使用1,2,3或4选择一个帐户。我正在使用do-while循环,所以我可以使用0退出(中断? )或再次运行循环,直到选择有效选项。
The problem is, Visual studio is telling me the code validAccount = true;
is unreachable, and won't let me test it. since this method should also return a value i have a "not all code paths return a value" error, and i am getting really confused. here is my code:
问题是,Visual studio告诉我代码validAccount = true;无法访问,不会让我测试它。因为这个方法也应该返回一个值,我有一个“并非所有代码路径都返回一个值”错误,我真的很困惑。这是我的代码:
static int chooseAccount() {
bool validAccount = false;
do {
Console.Clear();
Console.WriteLine("Select an Account: \r\n 1: Savings \r\n 2: Debit \r\n 3: Credit \r\n 4: Investment");
int inputNumber = int.Parse(Console.ReadLine()); //by declaring int inputNumber here we save having an extra line.
if ((inputNumber >= 1) && (inputNumber <= 4)) {
return inputNumber;
validAccount = true;
}
else if (inputNumber == 0) {
break;
}
else {
Console.WriteLine("Error: please choose an account using 1, 2, 3, 4 or 0 to exit");
}
} while (!validAccount);
}//end chooseAccount
Maybe I've just been staring at this for too long and can't see the simple mistake I've made. I welcome a fresh perspective or direction to where i can find a solution, should this sort of problem have already been solved. (it's kinda hard to google for something like "unreachable code" when the code has to be so specific...)
也许我只是盯着这个太久了,看不出我犯的那个简单的错误。如果这种问题已经解决,我欢迎有新的观点或方向,我可以找到解决方案。 (当代码必须如此具体时,谷歌有点像“无法访问的代码”这样的东西......)
3 个解决方案
#1
1
The line with the error comes right after a return statement. Your code would, in every single case (and the compiler knows this), exit the function on the line before it, so it will never reach the next line.
带有错误的行在return语句之后。你的代码在每一种情况下(并且编译器都知道这一点),在它之前的行上退出函数,因此它永远不会到达下一行。
You don't need to set validAccount
to true
to break the loop, because the return
statement will exit the function, and therefore automatically exit the loop.
您不需要将validAccount设置为true来中断循环,因为return语句将退出该函数,因此会自动退出循环。
And finally, if you set validAccount
to true and THEN return, no other function can access validAccount
, so why did you need to bother setting it to true?
最后,如果你将validAccount设置为true而THEN返回,那么没有其他函数可以访问validAccount,那么为什么你需要打扰将它设置为true?
In truth, you don't need validAccount
at all, because with your break
and return
statements you already control your way in and out of the loop. This will work:
实际上,您根本不需要validAccount,因为使用break和return语句,您已经控制了进出循环的方式。这将有效:
static int chooseAccount() {
while(true) {
Console.Clear();
Console.WriteLine("Select an Account: \r\n 1: Savings \r\n 2: Debit \r\n 3: Credit \r\n 4: Investment");
int inputNumber = int.Parse(Console.ReadLine());
if ((inputNumber >= 0) && (inputNumber <= 4)) {
return inputNumber;
}
else
{
Console.WriteLine("Error: please choose an account using 1, 2, 3, 4 or 0 to exit");
}
}
}
#2
0
Please be aware that the return
statement will take you out of the function immediately. Then the next validAccount = true;
will never be executed!
请注意,return语句会立即将您从函数中删除。然后下一个validAccount = true;永远不会被执行!
#3
0
Switch these two:
切换这两个:
return inputNumber;
validAccount = true;
#1
1
The line with the error comes right after a return statement. Your code would, in every single case (and the compiler knows this), exit the function on the line before it, so it will never reach the next line.
带有错误的行在return语句之后。你的代码在每一种情况下(并且编译器都知道这一点),在它之前的行上退出函数,因此它永远不会到达下一行。
You don't need to set validAccount
to true
to break the loop, because the return
statement will exit the function, and therefore automatically exit the loop.
您不需要将validAccount设置为true来中断循环,因为return语句将退出该函数,因此会自动退出循环。
And finally, if you set validAccount
to true and THEN return, no other function can access validAccount
, so why did you need to bother setting it to true?
最后,如果你将validAccount设置为true而THEN返回,那么没有其他函数可以访问validAccount,那么为什么你需要打扰将它设置为true?
In truth, you don't need validAccount
at all, because with your break
and return
statements you already control your way in and out of the loop. This will work:
实际上,您根本不需要validAccount,因为使用break和return语句,您已经控制了进出循环的方式。这将有效:
static int chooseAccount() {
while(true) {
Console.Clear();
Console.WriteLine("Select an Account: \r\n 1: Savings \r\n 2: Debit \r\n 3: Credit \r\n 4: Investment");
int inputNumber = int.Parse(Console.ReadLine());
if ((inputNumber >= 0) && (inputNumber <= 4)) {
return inputNumber;
}
else
{
Console.WriteLine("Error: please choose an account using 1, 2, 3, 4 or 0 to exit");
}
}
}
#2
0
Please be aware that the return
statement will take you out of the function immediately. Then the next validAccount = true;
will never be executed!
请注意,return语句会立即将您从函数中删除。然后下一个validAccount = true;永远不会被执行!
#3
0
Switch these two:
切换这两个:
return inputNumber;
validAccount = true;