C# 跳出包含 switch 语句的 while 循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1987379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Break out of a while loop that contains a switch statement
提问by joshblair
I am having trouble figuring out how to break out of a loop that contains a switch statement. Break breaks out of the switch, not the loop.
我无法弄清楚如何跳出包含 switch 语句的循环。Break 是跳出开关,而不是循环。
There is probably a more elegant solution to this. I have implemented a flag that starts out as true and gets set to false and ends the loop. Can you offer a better solution?
可能有一个更优雅的解决方案。我已经实现了一个标志,它开始为真,然后设置为假并结束循环。你能提供更好的解决方案吗?
Background: this code is used in a bar code workflow system. We have PocketPCs that have bar code scanners built in. This code is used in one of those functions. It prompts the user for different pieces of data throughout the routine. This piece allows them to scroll through some inventory records displaying that info on the PocketPC terminal (paged results) and allows them to enter "D" for Done, "Q" to quit.
背景:此代码用于条码工作流系统。我们有内置条形码扫描器的 PocketPC。此代码用于其中一项功能。它会在整个例程中提示用户输入不同的数据。这一段允许他们在 PocketPC 终端上滚动显示该信息的一些库存记录(分页结果),并允许他们输入“D”表示完成,“Q”退出。
Here is the current C# example that needs to be improved:
这是当前需要改进的 C# 示例:
do
{
switch (MLTWatcherTCPIP.Get().ToUpper())
{
case "": //scroll/display next inventory location
MLTWatcherTCPIP.TerminalPrompt.ScrollBodyTextDown();
break;
case "P": //scroll/display previous inventory location
MLTWatcherTCPIP.TerminalPrompt.ScrollBodyTextDown();
break;
case "D": //DONE (exit out of this Do Loop)
// break; // this breaks out of the switch, not the loop
// return; // this exists entire method; not what I'm after
keepOnLooping = false;
break;
case "Q": //QUIT (exit out to main menu)
return;
default:
break;
}
} while (keepOnLooping);
Here is an example of code that does this in VB.NET
这是在 VB.NET 中执行此操作的代码示例
Do
Select Case MLTWatcherTCPIP.Get().ToUpper
Case "" ''#scroll/display next inventory location
MLTWatcherTCPIP.TerminalPrompt.ScrollBodyTextDown()
Case "P" ''#scroll/display previous inventory location
MLTWatcherTCPIP.TerminalPrompt.ScrollBodyTextUp()
Case "D" ''#DONE (exit out of this Do Loop)
Exit Do
Case "Q" ''#QUIT (exit out to main menu)
Return
End Select
Loop
Thanks,
谢谢,
采纳答案by Jeffrey L Whitledge
I find this form to be ever-so-slightly more readable:
我发现这个表格更具可读性:
bool done = false;
while (!done)
{
switch (MLTWatcherTCPIP.Get().ToUpper())
{
case "": //scroll/display next inventory location
MLTWatcherTCPIP.TerminalPrompt.ScrollBodyTextDown();
break;
case "P": //scroll/display previous inventory location
MLTWatcherTCPIP.TerminalPrompt.ScrollBodyTextDown();
break;
case "D": //DONE (exit out of this Do Loop)
done = true;
break;
case "Q": //QUIT (exit out to main menu)
return;
default:
break;
}
}
回答by Tor Valamo
You must use a goto statement for multi level breaks. It appears to be the only 'clean' way in C#. Using a flag is also useful, but requires extra code if the loop has other predicaments for running.
您必须使用 goto 语句进行多级中断。它似乎是 C# 中唯一的“干净”方式。使用标志也很有用,但如果循环有其他运行困境,则需要额外的代码。
http://msdn.microsoft.com/en-us/library/aa664756(VS.71).aspx
http://msdn.microsoft.com/en-us/library/aa664756(VS.71).aspx
It may be interesting to note that some other non-c languages have multi level breaks by doing break levels;
(Java is just as useless, though, as it uses a goto disguised as a continue.. :P)
有趣的是,其他一些非 c 语言通过执行具有多级中断(Java 也同样无用,因为它使用伪装成继续的 goto .. :P)break levels;
回答by John Knoeller
A flag is the standard way to do this. The only other way I know of is to use a goto
.
标志是执行此操作的标准方法。我所知道的唯一另一种方法是使用goto
.
回答by Kevin Montrose
回答by McAden
The only other way I know of is the dreaded goto. MSDN also says this.
我所知道的唯一其他方法是可怕的 goto。 MSDN 也这么说。
However, I see no reason why you'd use it in this case. The way you have implemented works fine, and is more maintainable than a goto. I would keep what you have.
但是,我看不出您在这种情况下为什么要使用它。您实施的方式工作正常,并且比 goto 更易于维护。我会保留你所拥有的。
回答by Fry
IMO, this seems a perfectly fine way of breaking out of a while
loop. It does what you expect with no side effects. I could think of doing
IMO,这似乎是打破while
循环的完美方式。它可以满足您的预期而没有副作用。我可以想到做
if(!keepOnLooping)
break;
But that's not really any different in terms of execution.
但这在执行方面并没有什么不同。
回答by Marc Gravell
One option here is to refactor this loop into a method ("extract method"), and use return
.
这里的一个选择是将此循环重构为一个方法(“提取方法”),并使用return
.
回答by Hamish Grubijan
Wrap it into a function and use a return statement to exit. How about that?
将其包装成一个函数并使用 return 语句退出。那个怎么样?
回答by Chuck Conway
You could change the switch statement to a for/foreach loop. Once the condition is met set "keepOnLooping" to false and then use break to get out of the loop. The rest should take care of itself.
您可以将 switch 语句更改为 for/foreach 循环。一旦满足条件,将“keepOnLooping”设置为 false,然后使用 break 退出循环。其余的应该照顾好自己。
回答by Jeffrey Cameron
Why not wrap the switch into a method that returns a boolean to keep on looping? It would have the side benefit of making the code more readable. There's a reason someone wrote a paper saying we don't need goto statements after all ;)
为什么不将开关包装到一个返回布尔值的方法中以继续循环?它的附带好处是使代码更具可读性。有人写了一篇论文说我们毕竟不需要 goto 语句是有原因的;)
do
{
bool keepOnLooping = TryToKeepLooping();
} while (keepOnLooping);
private bool TryToKeepLooping()
{
switch (MLTWatcherTCPIP.Get().ToUpper())
{
case "": //scroll/display next inventory location
MLTWatcherTCPIP.TerminalPrompt.ScrollBodyTextDown();
break;
case "P": //scroll/display previous inventory location
MLTWatcherTCPIP.TerminalPrompt.ScrollBodyTextDown();
break;
case "D": //DONE (exit out of this Do Loop)
// break; // this breaks out of the switch, not the loop
// return; // this exists entire method; not what I'm after
return false;
case "Q": //QUIT (exit out to main menu)
return true;
default:
break;
}
return true;
}
回答by Chris R. Timmons
You can replace the switch
statement with an if/else
statement. No goto
needed and the break
statement leaves the loop:
您可以用switch
语句替换该if/else
语句。不需要goto
,break
语句离开循环:
do
{
String c = MLTWatcherTCPIP.Get().ToUpper();
if (c = "")
MLTWatcherTCPIP.TerminalPrompt.ScrollBodyTextDown();
else if (c = "P")
MLTWatcherTCPIP.TerminalPrompt.ScrollBodyTextUp();
else if (c = "D")
break;
else if (c = "Q")
return;
else
{
// Handle bad input here.
}
} while (keepLooping)