I'm trying to ask the user to enter a number higher than 0. When a negative number or a letter is entered, you get an error. I want the question to keep coming up until a correct input is entered. I tried doing that below, but I think I didn't lay it out correctly and missing some code.
double _heightVal = 0;
bool _userNotDone = true;
do
{
Console.Write("Please enter the first part of your height in feet:");
string height = Console.ReadLine();
if (!double.TryParse(height, out _heightVal))
{
Console.WriteLine("Error. You must enter a number. Please try again.");
}
else if (_heightVal < 0)
{
Console.WriteLine("Error. Number must be greater than 0. Please try again.");
}
else
{
_userNotDone = false;
}
}
while (_userNotDone == true || _heightVal < 0) ;
Aucun commentaire:
Enregistrer un commentaire