Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 22514

How do I implement exception Handling with while loops

$
0
0

I want the the program to ask the user to enter the number again and again until the format exception is not there.

static void Main(string[] args){    int userGuess1 = 0;    int userGuess2 = 0;    do    {        try        {            Console.WriteLine("Welcome to the game. To win User 2 must guess the number User 1 inputs!");            Console.WriteLine("User 1, Guess a number between 1 and 10 : ");            userGuess1 = Convert.ToInt32(Console.ReadLine());            Console.WriteLine("User 2, Guess a number between 1 and 10 : ");            userGuess2 = Convert.ToInt32(Console.ReadLine());            if (userGuess2 < userGuess1)            {                Console.WriteLine("Your guess is too low.");            }            else if (userGuess2 > userGuess1)            {                Console.WriteLine("Your guess is too high.");            }        }        catch (FormatException e)        {            Console.WriteLine("Please enter numbers between 1 and 10 only.");            Console.ReadKey();        }    }    while (userGuess1 != userGuess2);    Console.WriteLine("Congratulations! You won!");    Console.ReadKey();}

I cant get the program to loop again. It exits when there is an format exception. I did try moving try and catch statements but it generates errors.


Viewing all articles
Browse latest Browse all 22514

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>