Advertisement
stefanpu

Enter sequence

Feb 14th, 2013
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.00 KB | None | 0 0
  1. using System;
  2.  
  3. class EnterTenNumberInRange
  4. {
  5.     static void Main()
  6.     {
  7.         int countNumbers = 10;
  8.         int start = 1;
  9.         int end = 100;
  10.         int number;
  11.         Console.WriteLine("Please enter 10 integer numbers:");
  12.         for (int i = 0; i < countNumbers; i++)
  13.         {
  14.             // Чрез този цикъл можеш да се опитваш да вкараш число докато условието
  15.             //на задата е изпълнено
  16.            
  17.             do
  18.             {
  19.                 number =  ReadNumber(start, end);
  20.                 if (number!=0)
  21.                 {
  22.                     start = number;
  23.                 }
  24.  
  25.                 //Допълнителна проверка.
  26.                 //Пример:
  27.                 /*
  28.                  * Първо число - 6
  29.                  * Второ число - 93
  30.                  * Трябва да вуведеш още 8 числа, а в най- добрия случай
  31.                  * може да въведеш: 94, 95, 96, 97, 98, 99 -> само 6 числа
  32.                  * Няма смисъл програмата да продължава.
  33.                  */
  34.                 if (number +(countNumbers - i)>100)
  35.                 {
  36.                     Console.WriteLine("Not possible to enter whole sequnece");
  37.                     Console.WriteLine("Exiting...");
  38.                     return;
  39.                 }
  40.                
  41.             } while (number==0);
  42.                      
  43.         }
  44.  
  45.     }
  46.     static int ReadNumber(int start, int end)
  47.     {
  48.         try
  49.         {
  50.             int number = int.Parse(Console.ReadLine());
  51.             if (number <= start || number >= end)
  52.             {
  53.                 //Съобщението, което ще се изписва трябва да взима под внимание
  54.                 //текущата стойност на start.
  55.                 string message = "The number in not in the range (" + start + ";100)";
  56.  
  57.                 //null е името на параметъра хвърляш изключението.
  58.                 //Ако не е null тогава и името на параметъра ще бъде изписано.
  59.                 throw new ArgumentOutOfRangeException(null,message);
  60.             }
  61.             Console.WriteLine("The number is VALID.");
  62.             return number;
  63.         }
  64.         catch (ArgumentOutOfRangeException  aoore)
  65.         {
  66.             Console.WriteLine(aoore.Message);
  67.             return 0;
  68.         }
  69.         catch (FormatException)
  70.         {
  71.             Console.WriteLine("The number in not in valid format!");
  72.             return 0;
  73.         }
  74.         catch (OverflowException)
  75.         {
  76.             Console.WriteLine("The number is too big !");
  77.             return 0;
  78.         }
  79.         catch (ArgumentNullException)
  80.         {
  81.             Console.WriteLine("You have entered nothing!");
  82.             return 0;
  83.         }
  84.  
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement