+ 1
Force an user to pick a valid option c#
How can I make an user chose a valid option to run the code and ask to re-input the value if he doesn't in let's say... an if statement? example class Car{ public String color; } class Ford:Car{ public Ford(); color = "red"; } class Renault:Car{ public Renault(); color = blue; { Car MyNewCar; //undefined object int x; x = Convert.ToInt32(Console.ReadLine()); if (x == 1){ Car MyNewCar = Ford; } else if (x==2) { Car MyNewCar = new Renault; } how do I force them to pick a number again if they don't put 1 or 2?
2 Answers
+ 1
Tell me if something is not clear, I omitted the writeline parts so it would fit in the character limits. that's a code I just made for the question btw it's probabbly wrong but I hope you get what I'm asking
0
Use a while loop.
While input is not 1 or 2, prompt for input.