在switch语句中使用while循环

本文关键字:while 循环 switch 语句 | 更新日期: 2023-09-27 18:01:19

我是c#新手,所以请原谅我。我想在默认的switch语句中使用while循环,但我不知道如何为while设置bool。我的目标是让菜单循环,直到1或2被按下。

switch (userChoice1)
{
    case "1":
        msg = "'n'nYou have chosen the House Salad with Ranch Dressing. 'nPress enter to continue.";
        saladChoice = "House Salad with Ranch Dressing";
        break;
    case "2":
        msg = "'n'nYou have chosen the Caesar Salad. 'nPress enter to continue. ";
        saladChoice = "Caesar Salad";
        break;
    default:
        msg = "'n'nYou have chosen an invalid option. You should have selected  'n1 for the House Salad 'nor 'n2 for the Caesar Salad. ";
        Console.Beep();
        while (true) // I don't know if the switch should be in the while loop or here
        {
        }
        break;
}

在switch语句中使用while循环

类似于

    bool valid;
    do
    {
        valid = true;
        userChoice1 = Console.ReadLine();
        switch (userChoice1)
        {
            case "1":
                msg = "'n'nYou have chosen the House Salad with Ranch Dressing. 'nPress enter to continue.";
                saladChoice = "House Salad with Ranch Dressing";
                break;
            case "2":
                msg = "'n'nYou have chosen the Caesar Salad. 'nPress enter to continue. ";
                saladChoice = "Caesar Salad";
                break;
            default:
                msg = "'n'nYou have chosen an invalid option. You should have selected  'n1 for the House Salad 'nor 'n2 for the Caesar Salad. ";
                valid = false;
                Console.Beep();
                break;
        }
    } while (!valid);

首先,不认真知道自己在做什么的情况下,绝不要while (true)。在这样的循环中,应用程序/线程将无法正确终止。

第二,你需要在某个bool值的开关外循环,比如:

bool validSelection = false;
while (!validSelection)
{
    string userSelection = Console.ReadLine();
    switch (userSelection)
    {
        case "1":
           validSelection = true;
           break;
        default:
           break;
    }
}

你需要将while循环置于switch之外…

bool validVal = false;
while (!validVal )
    {
    switch (userChoice1)
        {
        case "1":
            msg = "'n'nYou have chosen the House Salad with Ranch Dressing. 'nPress enter to continue.";
            saladChoice = "House Salad with Ranch Dressing";
            validVal = true;
            break;
        case "2":
            msg = "'n'nYou have chosen the Caesar Salad. 'nPress enter to continue. ";
            saladChoice = "Caesar Salad";
            validVal = true;
            break;
        default:
            msg = "'n'nYou have chosen an invalid option. You should have selected  'n1 for the House Salad 'nor 'n2 for the Caesar Salad. ";
            Console.Beep();
            break;
        }
    }

你的开关应该在while循环中。while循环所查看的变量应该在while循环之外声明,并且您将操纵它来确定是否在while循环之后继续执行。

bool proceed = false;
while(!proceed)
{
   //probably get userChoice1 from console here?
   switch (userChoice1)
   {
        case "1":
            msg = "'n'nYou have chosen the House Salad with Ranch Dressing. 'nPress enter to continue.";
            saladChoice = "House Salad with Ranch Dressing";
            proceed = true;
            break;
        case "2":
            msg = "'n'nYou have chosen the Caesar Salad. 'nPress enter to continue. ";
            saladChoice = "Caesar Salad";
            proceeed = true;
            break;
         default:
             msg = "'n'nYou have chosen an invalid option. You should have selected  'n1 for the House Salad 'nor 'n2 for the Caesar Salad. ";
             Console.Beep();               
             break;
    }
}

我不懂c#,所以请原谅语法错误。你可以把switch语句放在一个函数中,完全摆脱while循环,并在默认情况下递归地调用这个函数。

public void MenuSwitch()
{
   switch (userChoice1)
   {
        case "1":
            ...........
            break;
        case "2":
            ...........
            break;
        default:
            ...........
            MenuSwitch();
            break;
    }
}