Advertisement
PIBogdanov

02. Enter Numbers

Jul 3rd, 2023
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.01 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. int currentIndex = 0;
  10.  
  11. while (currentIndex != validNumbers)
  12. {
  13.     try
  14.     {
  15.         numbers[currentIndex] = ReadNumber(start, end);
  16.  
  17.         start = numbers[currentIndex];
  18.  
  19.         currentIndex++;
  20.     }
  21.  
  22.     catch (ArgumentOutOfRangeException argumentOutOfRangeException)
  23.     {
  24.         Console.WriteLine(argumentOutOfRangeException.Message);
  25.     }
  26.  
  27.     catch (FormatException formatException)
  28.     {
  29.         Console.WriteLine(formatException.Message);
  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