简体   繁体   English

尝试使用无效输入(C#)循环切换开关

[英]Trying to loop a switch with an invalid input (C#)

Anyway I don't know how I'm supposed to go about doing this... in my application I need to have the switch repeat itself if there's an invalid input. 无论如何,我不知道该怎么做...在我的应用程序中,如果输入无效,我需要让开关自己重复一次。 All the application does is exit as soon as I enter a different result. 我输入其他结果后,所有应用程序都会退出。 Here's my code: 这是我的代码:

string str = Console.ReadLine(); 
char option = char.Parse(str);
//Need to repeat this switch:
switch (option)
{
    case 'N':
        Console.WriteLine("Creating New App...");
        break;
    case 'L':
        Console.WriteLine("Loading App...");
        break;
    case 'O':
        Console.WriteLine("Loading Options...");
        break;
    case 'Q':
        Console.WriteLine("Exiting Application...");
        System.Environment.Exit(1);
        break;
    default:
        Console.WriteLine("Invalid input.");
        break;
}
Console.ReadLine();

You could make use of a while statement. 您可以使用while语句。

bool shouldRun = true;

while(shouldRun)
{
    switch (option)
    {
        case 'N':
            Console.WriteLine("Creating New App...");
            shouldRun = false;
            break;
        case 'L':
            Console.WriteLine("Loading App...");
            shouldRun = false;
            break;
        case 'O':
            Console.WriteLine("Loading Options...");
            shouldRun = false;
            break;
        case 'Q':
            Console.WriteLine("Exiting Application...");
            System.Environment.Exit(1);
            break;
        default:
            Console.WriteLine("Invalid input.");
            shouldRun = true;
            break;
    }
}

Put the switch in a loop switch循环

bool invalidInput = true;

while (invalidInput)
{
    string str = Console.ReadLine(); 
    char option = char.Parse(str);

    switch (option)
    {
        case 'N':
            Console.WriteLine("Creating New App...");
            invalidInput = false;
            break;
        case 'L':
            Console.WriteLine("Loading App...");
            invalidInput = false;
            break;
        case 'O':
            Console.WriteLine("Loading Options...");
            invalidInput = false;
            break;
        case 'Q':
            Console.WriteLine("Exiting Application...");
            System.Environment.Exit(1);
            break;
        default:
            Console.WriteLine("Invalid input.");
            break;
    }
}

To repeat the switch you need to use one loop.I think you are looking for something like the following: 要重复切换,您需要使用一个循环。我认为您正在寻找类似以下内容的东西:

    bool ExitFlag = true;
    while (ExitFlag)
    {
        ExitFlag = false;
        switch (option)
        {
            case 'N':
                Console.WriteLine("Creating New App...");
                break;
            case 'L':
                Console.WriteLine("Loading App...");
                break;
            case 'O':
                Console.WriteLine("Loading Options...");
                break;
            case 'Q':
                Console.WriteLine("Exiting Application...");
                System.Environment.Exit(1);
                break;
            default:
                Console.WriteLine("Invalid input.");
                ExitFlag = true;
                break;
        }
    }

Note How It Works : 注意它是如何工作的

Let the ExitFlag be a Boolean value that controls the while loop( stop iteration and exit the while when ExitFlag is false). ExitFlag为一个控制while循环的布尔值(当ExitFlag为false时,停止迭代并退出while)。 and are initialized with true . 并使用true初始化。 in each time the control enters into the while the flag is set to false so that we can avoid setting it false in multiple cases. 每次控件进入while时,将标志设置为false这样我们就可以避免在多种情况下将其设置为false。 The flag will set to true only when the default case is executed(ie., the invalid output) hence the loop repeats in this condition only. 仅当执行default情况(即无效输出)时,该标志才会设置为true ,因此循环仅在这种情况下重复。

You can use the Goto: label option in C#. 您可以在C#中使用Goto:标签选项。

Ref: https://msdn.microsoft.com/en-us/library/13940fs2.aspx 参考: https : //msdn.microsoft.com/zh-cn/library/13940fs2.aspx

    ReadAgain:
    string str = Console.ReadLine(); 
    char option = char.Parse(str);

    //The switch itself:
    switch (option)
    {
        case 'N':
            Console.WriteLine("Creating New App...");
            break;
        case 'L':
            Console.WriteLine("Loading App...");
            break;
        case 'O':
            Console.WriteLine("Loading Options...");
            break;
        case 'Q':
            Console.WriteLine("Exiting Application...");
            System.Environment.Exit(1);
            break;
        default:
            Console.WriteLine("Invalid input.");
            goto ReadAgain;
            break;
    }
    Console.ReadLine();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM