Last active
May 17, 2023 06:45
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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