I've already made it so that it can detect negative numbers and numbers over 100. That was easy enough. My issue comes in when you input a string. If you do that, it'll crash. While funny, I don't want it to do that, I want it to spit out an error message and have the user try again like I have for the other case. But I'm having a very hard time trying to figure that out.
Console.Write("First Grade: ");
firstGrade = Convert.ToDouble(Console.ReadLine());
while (firstGrade > 100 || firstGrade < 0)
{
Console.Write("Please input a number that is greater than or equal to 0 but less than or equal to 100: ");
firstGrade = Convert.ToDouble(Console.ReadLine());
}
Console.WriteLine();
CodePudding user response:
A minor change, using double.TryParse
(as suggested by @Dmitry):
double firstGrade;
Console.Write("First Grade: ");
while (!double.TryParse(Console.ReadLine(), out firstGrade) || firstGrade > 100 || firstGrade < 0)
{
Console.Write(@"Please input a number that is greater than or equal to 0 but less than or equal to 100: ");
}
Console.WriteLine($@"You entered: {firstGrade}");