Advertisement
bonumopus

checknumber

Apr 19th, 2020
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace checknumber
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int result = CheckNumber();
  14.             Console.WriteLine("Вы ввели число: " + result);
  15.         }
  16.  
  17.         static int CheckNumber()
  18.         {
  19.             string userInput;
  20.             int result = int.MinValue;
  21.             bool numberEntered = false;
  22.  
  23.             while (!numberEntered)
  24.             {
  25.                 Console.Write("Введите число: ");
  26.                 userInput = Console.ReadLine();
  27.                 if (int.TryParse(userInput, out result))
  28.                 {
  29.                     numberEntered = true;
  30.                 }
  31.                 else
  32.                 {
  33.                     WriteError();
  34.                 }
  35.             }
  36.             return result;
  37.         }
  38.         static void WriteError()
  39.         {
  40.             ConsoleColor defaultColor = Console.ForegroundColor;
  41.             Console.ForegroundColor = ConsoleColor.Red;
  42.             Console.WriteLine("Это не число!");
  43.             Console.ForegroundColor = defaultColor;
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement