Skip to content

Instantly share code, notes, and snippets.

@Strelok78
Last active May 17, 2023 06:45
Show Gist options
  • Save Strelok78/a680e1fbda56f989c864b97db1f5a6c1 to your computer and use it in GitHub Desktop.
Save Strelok78/a680e1fbda56f989c864b97db1f5a6c1 to your computer and use it in GitHub Desktop.
Requests a number from the user and tries to convert it to the int type. If the conversion failed, the user is asked for the number again until it is entered correctly. After the input that was converted to a number, the number is returned.
internal class Program
{
static void Main(string[] args)
{
int number;
number = GetNumber();
Console.WriteLine(number);
}
static int GetNumber()
{
int parsedNumber;
while (int.TryParse(Console.ReadLine(), out parsedNumber) == false)
{
Console.WriteLine("Try again!");
}
return parsedNumber;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment