Created
November 4, 2019 11:03
-
-
Save FloydanTheBeast/559058d340e422f821ae7b62cdf438e8 to your computer and use it in GitHub Desktop.
Integer input with predicate as a parameter
This file contains 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
/// <summary> | |
/// Метод для ввода целочисленного значения | |
/// </summary> | |
/// <param name="isCorrect">Предикат, проверяющий правильность ввода</param> | |
/// <returns>Введённое из консоли число</returns> | |
public static int IntInput(Func<int, bool> isCorrect = null, string msg = null) | |
{ | |
if (msg != null) | |
{ | |
Console.WriteLine(msg); | |
} | |
int number; | |
if (!int.TryParse(Console.ReadLine(), out number) || (isCorrect != null && !isCorrect(number))) | |
{ | |
Console.Write("Некорректный ввод"); | |
Environment.Exit(0); | |
} | |
return number; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment