小编jos*_*air的帖子

打破包含switch语句的while循环

我无法弄清楚如何打破包含switch语句的循环.断开了开关,而不是循环.

对此可能有更优雅的解决方案.我已经实现了一个标志,该标志以true开头并设置为false并结束循环.你能提供更好的解决方案吗?

背景:此代码用于条形码工作流程系统.我们有内置条形码扫描仪的PocketPC.此代码用于其中一个功能.它会在整个例程中提示用户输入不同的数据.这件作品允许他们滚动显示在PocketPC终端上显示该信息的一些库存记录(分页结果)并允许他们输入"D"表示完成,"Q"表示退出.

这是当前需要改进的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);
Run Code Online (Sandbox Code Playgroud)

这是在VB.NET中执行此操作的代码示例

Do
    Select Case …
Run Code Online (Sandbox Code Playgroud)

c# break while-loop

54
推荐指数
7
解决办法
5万
查看次数

标签 统计

break ×1

c# ×1

while-loop ×1