Advertisement
Guest User

Untitled

a guest
Nov 24th, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. using System;
  2.  
  3. class EnterNumbers
  4. {
  5. static void Main(string[] args)
  6. {
  7. for (int i = 0; i < 10; i++)
  8. {
  9. Console.WriteLine(EnterNumber(1, 100));
  10. }
  11. }
  12.  
  13. static int EnterNumber(int start, int end)
  14. {
  15. try
  16. {
  17. return ReadNumber(start, end);
  18. }
  19. catch (FormatException ex)
  20. {
  21. Console.WriteLine(ex.Message);
  22. Console.WriteLine("Try again: ");
  23. return EnterNumber(start, end);
  24. }
  25. catch (ArgumentNullException ex)
  26. {
  27. Console.WriteLine(ex.Message);
  28. Console.WriteLine("Try again: ");
  29. return EnterNumber(start, end);
  30.  
  31. }
  32. catch (ArgumentOutOfRangeException ex)
  33. {
  34. Console.WriteLine(ex.Message);
  35. Console.WriteLine("Try again: ");
  36. return EnterNumber(start, end);
  37. }
  38. }
  39.  
  40. static int ReadNumber(int start, int end)
  41. {
  42. string str = Console.ReadLine();
  43. if (string.IsNullOrEmpty(str))
  44. {
  45. throw new ArgumentNullException("value", "The number cannot be null or empty");
  46. }
  47. try
  48. {
  49. int n = int.Parse(str);
  50.  
  51. if (n < start || n > end)
  52. {
  53. throw new ArgumentOutOfRangeException(
  54. "value",
  55. string.Format("The value must be in the range [{0}...{1}]", start, end));
  56. }
  57. return n;
  58. }
  59. catch (FormatException)
  60. {
  61. throw new FormatException("The entered value in not a number and cannot be parsed");
  62. }
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement