Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int start = 1;
- int end = 100;
- int validNumbers = 10;
- int[] numbers = new int[validNumbers];
- int currentIndex = 0;
- while (currentIndex != validNumbers)
- {
- try
- {
- numbers[currentIndex] = ReadNumber(start, end);
- start = numbers[currentIndex];
- currentIndex++;
- }
- catch (ArgumentOutOfRangeException argumentOutOfRangeException)
- {
- Console.WriteLine(argumentOutOfRangeException.Message);
- }
- catch (FormatException formatException)
- {
- Console.WriteLine(formatException.Message);
- }
- }
- Console.Write(string.Join(", ", numbers));
- static int ReadNumber(int start, int end)
- {
- if (int.TryParse(Console.ReadLine(), out int number))
- {
- if ( (number <= start) || (number >= end) )
- {
- throw new ArgumentOutOfRangeException(null, $"Your number is not in range {start} - {end}!");
- }
- }
- else
- {
- throw new FormatException("Invalid Number!");
- }
- return number;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement