I can’t get out of the While Loop in my code and out of the method. The user has the option to enter 1 or 2 and 0 to cancel(to leave the menu and the method). But as the code are now, I guess the default option in the Switch prevent it from leaving the While Loop!? Can I do it in another and better way?
// Read user input
public void ReadUserInput()
{
Console.Write("Your choice? ");
int choice = -1;
while (choice != 0)
{
if (int.TryParse(Console.ReadLine(), out choice))
{
switch (choice)
{
case 1:
ShowSchedule(1);
break;
case 2:
ShowSchedule(2);
break;
default:
ErrorMessage(); // Call method to show error message
ReadUserInput(); // Call method for new input
break;
}
}
else
{
// Call method to show error message
ErrorMessage();
Start();
}
}
}
Just add a
to exit the loop if the user inputs 0. If there is indeed no code that should run after the loop in the method, you can also directly return from it with