Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class EnterNumbers
- {
- static void Main(string[] args)
- {
- for (int i = 0; i < 10; i++)
- {
- Console.WriteLine(EnterNumber(1, 100));
- }
- }
- static int EnterNumber(int start, int end)
- {
- try
- {
- return ReadNumber(start, end);
- }
- catch (FormatException ex)
- {
- Console.WriteLine(ex.Message);
- Console.WriteLine("Try again: ");
- return EnterNumber(start, end);
- }
- catch (ArgumentNullException ex)
- {
- Console.WriteLine(ex.Message);
- Console.WriteLine("Try again: ");
- return EnterNumber(start, end);
- }
- catch (ArgumentOutOfRangeException ex)
- {
- Console.WriteLine(ex.Message);
- Console.WriteLine("Try again: ");
- return EnterNumber(start, end);
- }
- }
- static int ReadNumber(int start, int end)
- {
- string str = Console.ReadLine();
- if (string.IsNullOrEmpty(str))
- {
- throw new ArgumentNullException("value", "The number cannot be null or empty");
- }
- try
- {
- int n = int.Parse(str);
- if (n < start || n > end)
- {
- throw new ArgumentOutOfRangeException(
- "value",
- string.Format("The value must be in the range [{0}...{1}]", start, end));
- }
- return n;
- }
- catch (FormatException)
- {
- throw new FormatException("The entered value in not a number and cannot be parsed");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement