简体   繁体   中英

default statement in a switch, in while loop, not breaking c#

basic card game, have a switch statement in a while loop. case is card number selected by user input, then replaced. that part is working fine. but testing the default statement (any number not 0-5), and it goes into an infinite loop of the default console.writeline there and I'm not sure why its not breaking. in previous switches nested in while loops its worked for me but ive never had this infinite loop issue on previous defaults before. any help/tips?

int userInput;
string userString;
Console.WriteLine("Would you like to replace a card?");
Console.WriteLine("Select which card you would like to replace, 1-5. Enter 0 to skip");
userString = Console.ReadLine();
int.TryParse(userString, out userInput);
while (userInput != 0)
{
    switch (userInput)
    {
        case 1:
            userInput--;
            userHand[userInput] = cardDeck.GetOneCard();
            break;
        case 2:
            userInput--;
            userHand[userInput] = cardDeck.GetOneCard();
            break;
        case 3:
            userInput--;
            userHand[userInput] = cardDeck.GetOneCard();
            break;
        case 4:
            userInput--;
            userHand[userInput] = cardDeck.GetOneCard();
            break;
        case 5:
            userInput--;
            userHand[userInput] = cardDeck.GetOneCard();
            break;
        default:
            Console.WriteLine("Incorrect input, try again");
            break;
    }
}

Problem: You are not reading the user input in while loop .

if you don't read the user input in while loop it works for only one iteration and if the choosen number is between 1-5 it's fine but if choosen number is some thing other than 1-5 it goes to default case and as you are not reading the userinput it remains same so it will enter into infinite loop .

Solution :

Place The following statements inside while loop to read the userInput

userString = Console.ReadLine();
int.TryParse(userString, out userInput);

Complete Solution:

int userInput;
string userString;
Console.WriteLine("Would you like to replace a card?");
Console.WriteLine("Select which card you would like to replace, 1-5. Enter 0 to skip");
userString = Console.ReadLine();
int.TryParse(userString, out userInput);
while (userInput != 0)
{
    switch (userInput)
    {
        case 1:
            userInput--;
            userHand[userInput] = cardDeck.GetOneCard();
            break;
        case 2:
            userInput--;
            userHand[userInput] = cardDeck.GetOneCard();
            break;
        case 3:
            userInput--;
            userHand[userInput] = cardDeck.GetOneCard();
            break;
        case 4:
            userInput--;
            userHand[userInput] = cardDeck.GetOneCard();
            break;
        case 5:
            userInput--;
            userHand[userInput] = cardDeck.GetOneCard();
            break;
        default:
            Console.WriteLine("Incorrect input, try again");
            break;
    }
      Console.WriteLine("Select which card you would like to replace, 1-5. Enter 0 to skip");
      userString = Console.ReadLine();
      int.TryParse(userString, out userInput);
}

It's because the default case doesn't change the value of userInput , so userInput is always not 0 and the loop is infinite. You can fix it by prompting the user for a new number:

default:
    Console.WriteLine("Incorrect input, try again");
    int.TryParse(Console.ReadLine(), out userInput);
    break;

This if you want to modify the default case only. Otherwise, to prompt the user for a new number at every loop, use Sudhakar's solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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