While Loop in C# with Switch Statement

点点圈 提交于 2021-01-28 14:08:08

问题


I am trying to loop the switch condition if the choice is out of range. But i am not getting the desired output. So if while going through the switch condition the user does not input 1-3 as input, i want it to go to default condition which should trigger the error statement and then keep looping

Console.WriteLine("Which book would you like to check out?");
Console.WriteLine("select 1 for book 1, 2 for book 2, and 3 for book 3");

int choice=Convert.ToInt32(Console.ReadLine());

bool loopBreak = true;
while (true)
{
    switch (choice)
    {

        case 1:
            Console.WriteLine("You have chosen book 1 {0}", b1.name);
            b1.CheckinCheckout = false;

            break;


        case 2:
            Console.WriteLine("You have chosen book 2 {0}", b2.name);
            b2.CheckinCheckout = false;

            break;

        case 3:
            Console.WriteLine("You have chosen book 3 {0}", b3.name);
            b3.CheckinCheckout = false;

            break;
        default:
            Console.WriteLine("Please enter a valid choice.");
            loopBreak = false;
            break;
    }
    if (loopBreak != false)
    {

        break;
    }
}

Updated:

bool loopBreak=true;
while (loopBreak==true)
{
    Console.WriteLine("Which book would you like to check out?");
    Console.WriteLine("select 1 for book 1, 2 for book 2, and 3 for book 3");
    int choice = Convert.ToInt32(Console.ReadLine());
    switch (choice)
    {

        case 1:
            Console.WriteLine("You have chosen book 1 {0}", b1.name);
            b1.CheckinCheckout = false;

            break;


        case 2:
            Console.WriteLine("You have chosen book 2 {0}", b2.name);
            b2.CheckinCheckout = false;

            break;

        case 3:
            Console.WriteLine("You have chosen book 3 {0}", b3.name);
            b3.CheckinCheckout = false;

            break;
        default:
            Console.WriteLine("Please enter a valid choice.");
            loopBreak = false;
            break;
    }
    break;
}

回答1:


You need to set the boolean to false when you want to exit from the loop and do nothing when you want to continue asking for a valid input.

loopContinue = true;
while (loopContinue)
{
    Console.WriteLine("Which book would you like to check out?");
    Console.WriteLine("select 1 for book 1, 2 for book 2, and 3 for book 3");
    // Use TryParse when reading the user input. This will avoid an 
    // Exception if the user types a letter for example.
    if(Int32.TryParse(Console.ReadLine(), int out choice)
    {
        switch (choice)
        {
            case 1:
               ....
               loopContinue = false;
               break;
            case 2:
               ....
               loopContinue = false;
               break;
            case 3:
               ....
               loopContinue = false;
               break;

            // not really needed, if you remove the default
            // then your loop will not exit and you can start again
            default:
               loopContinue = true;
               break;
    }
    if(loopContinue)
         Console.WriteLine("Please enter a valid choice.");
}


来源:https://stackoverflow.com/questions/47251045/while-loop-in-c-sharp-with-switch-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!