在交换机语句中使用while循环

时间:2015-07-06 17:19:34

标签: c#

我是C#的新手并且编码一般,所以请原谅我。我想在我的while语句的默认值中使用switch循环,但我不知道如何为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;
}

5 个答案:

答案 0 :(得分:5)

类似的东西:

    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);

答案 1 :(得分:3)

首先,永远写<{1}},不要认真知道你在做什么。应用程序/线程不会在这样的循环中正确终止。

其次,你需要在开关外面循环一些bool,例如:

while (true)

答案 2 :(得分:2)

您需要在交换机外部使用while循环...

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;
        }
    }

答案 3 :(得分:1)

你的开关应该在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;
    }
}

答案 4 :(得分:1)

我不知道C#,所以请原谅语法错误。你可以将switch语句放在一个完全摆脱while循环的函数中,并以递归方式调用默认情况下的函数。

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