Advertisement
Guest User

EnterNumbers

a guest
Sep 22nd, 2014
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class EnterNumbers
  5. {
  6.     public static int? ReadNumber(ref int start, ref int end)
  7.     {
  8.         Console.Write("Enter an integer [{0}..{1}] inclusive: ", start, end);
  9.         try
  10.         {
  11.             int n = int.Parse(Console.ReadLine());
  12.  
  13.             if (n < start || n > end) throw new ArgumentOutOfRangeException();
  14.             if (n == end)
  15.             {
  16.                 end = n - 1;
  17.                 return n;
  18.             }
  19.  
  20.             if (n + 1 <= end)
  21.             {
  22.                 start = n + 1;
  23.                 return n;
  24.             }
  25.  
  26.             return null;
  27.         }
  28.         catch (ArgumentOutOfRangeException)
  29.         {
  30.             return null;
  31.         }
  32.         catch (ArgumentNullException)
  33.         {
  34.             return null;
  35.         }
  36.         catch (FormatException)
  37.         {
  38.             return null;
  39.         }
  40.         catch (OverflowException)
  41.         {
  42.             return null;
  43.         }
  44.     }
  45.  
  46.     public static void Main(string[] args)
  47.     {
  48.         int start = 1;
  49.         int end = 100;
  50.         int count = 0;
  51.         List<int> numList = new List<int>();
  52.  
  53.         while (count < 10)
  54.         {
  55.             int? n = ReadNumber(ref start, ref end);
  56.  
  57.             if (n.HasValue)
  58.             {
  59.                 count++;
  60.                 numList.Add((int)n);
  61.             }
  62.  
  63.             if (start > end)
  64.             {
  65.                 Console.WriteLine("No more integers to choose from. Range length is zero.");
  66.                 break;
  67.             }
  68.         }
  69.        
  70.         Console.WriteLine("Finnished. {0} integers have been chosen", count);
  71.         string implode = string.Join(", ", numList.ToArray());
  72.         Console.WriteLine(implode);
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement