Question: I have the user inputting three numbers, I'm checking for exceptions but I'm having issues reasking the user to input a number if input was

I have the user inputting three numbers, I'm checking for exceptions but I'm having issues reasking the user to input a number if input was invalid ( it just continues on)

Example:

Enter First num: "putting in a string"

Brings an invalid exception

But instead of reasking the user to put in the first num, it continues on to asking them about the second number.

public static Test() { Console.WriteLine("Enter the first num"); int num1 = -1; try { num1 = Convert.ToInt16(Console.ReadLine()); } catch (Exception e) { Console.WriteLine("Invalid"); Console.ReadLine(); } Console.WriteLine("Enter the second num"); int num2 = -1; try { num2 = Convert.ToInt16(Console.ReadLine()); } catch (Exception e) { Console.Clear(); Console.WriteLine("Invalid"); Console.ReadLine(); } Console.WriteLine("Enter the third num"); int num3 = -1; try { num3 = Convert.ToInt16(Console.ReadLine()); } catch (Exception e) { Console.WriteLine("Invalid"); Console.ReadLine(); }

//rest of code here

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!