Advertisement
PIBogdanov

02. Enter Numbers

Jul 3rd, 2023
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.98 KB | None | 0 0
  1. int start = 1;
  2.  
  3. int end = 100;
  4.  
  5. int validNumbers = 10;
  6.  
  7. int[] numbers = new int[validNumbers];
  8.  
  9. for (int i = 0; i < validNumbers; i++)
  10. {
  11.     try
  12.     {
  13.         numbers[i] = ReadNumber(start, end);
  14.  
  15.         start = numbers[i];
  16.     }
  17.  
  18.     catch (ArgumentOutOfRangeException argumentOutOfRangeException)
  19.     {
  20.         Console.WriteLine(argumentOutOfRangeException.Message);
  21.  
  22.         i--;
  23.     }
  24.  
  25.     catch (FormatException formatException)
  26.     {
  27.         Console.WriteLine(formatException.Message);
  28.  
  29.         i--;
  30.     }
  31. }
  32.  
  33. Console.Write(string.Join(", ", numbers));
  34.  
  35. static int ReadNumber(int start, int end)
  36. {
  37.     if (int.TryParse(Console.ReadLine(), out int number))
  38.     {
  39.         if ( (number <= start) || (number >= end) )
  40.         {
  41.             throw new ArgumentOutOfRangeException(null, $"Your number is not in range {start} - {end}!");
  42.         }
  43.     }
  44.  
  45.     else
  46.     {
  47.         throw new FormatException("Invalid Number!");
  48.     }
  49.  
  50.     return number;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement