Created
February 8, 2022 16:04
-
-
Save jcollard/dd51585254ef7b9695df2053ff04eb4b to your computer and use it in GitHub Desktop.
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
using System; | |
public class ReadValidInputExample | |
{ | |
/// Summary: | |
/// Prompts the user to enter an integer that is greater than 1000. Then, | |
/// returns the users input as an int. If the user enters an invalid valud, | |
/// this method will continue to prompt the user. | |
/// | |
/// Returns: | |
/// The users input as an integer | |
public static int GetInputGreaterThan1000() | |
{ | |
// Create a variable to store the value the user enters | |
int userChoice; | |
// Start a loop | |
do | |
{ | |
// Asks the user to enter a number | |
Console.Write("Enter a number that is greater than 1000:"); | |
// Read the user input into a string | |
string input = Console.ReadLine(); | |
// Try to conver the user input into an integer. | |
bool isANumber = int.TryParse(input, out userChoice); | |
if (isANumber == false) | |
{ | |
// If the user didn't enter an integer, display an error. | |
Console.Error.WriteLine("You did not enter a number."); | |
} | |
else if (userChoice <= 1000) | |
{ | |
// If the user entered an integer that was not greater than 1000, | |
// display an error | |
Console.WriteLine("That number is not greater than 1000."); | |
} | |
} | |
while (userChoice <= 1000); // If the userChoice is less than 1000, we continue the loop | |
// Finally, we return the users choice | |
return userChoice; | |
} | |
public static void Main() | |
{ | |
int value = GetInputGreaterThan1000(); | |
Console.WriteLine($"You entered {value}."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment