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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started