Advertisement
BorisSimeonov

AgeNowAndAfter

Oct 21st, 2014
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.82 KB | None | 0 0
  1. using System;
  2.  
  3. class AgeNowAndAfter
  4. {
  5.     static void Main(string[] args)
  6.     {
  7.         int year = 0, month = 0, day = 0;
  8.  
  9.         //get the current date values
  10.         DateTime today = DateTime.Today;
  11.  
  12.         //initialize the birthday values in the corresponding inteher variables
  13.         for (int cnt = 0; cnt < 3; cnt++)
  14.         {
  15.             bool check = false;
  16.             switch (cnt)
  17.             {
  18.                 case 0:
  19.                     Console.WriteLine("Enter your year of birth here /ex. 2014/: ");
  20.                     check = int.TryParse(Console.ReadLine(), out year);
  21.                     if (!check || year < 1900 || year > today.Year)
  22.                     {
  23.                         cnt--;
  24.                         Console.Clear();
  25.                         Console.WriteLine("Invalid Input. Try Again.");
  26.                     }
  27.                     break;
  28.                 case 1:
  29.                     Console.WriteLine("Enter your month of birth here /ex. 9/: ");
  30.                     check = int.TryParse(Console.ReadLine(), out month);
  31.                     if (!check || month < 1 || month > 12)
  32.                     {
  33.                         cnt--;
  34.                         Console.Clear();
  35.                         Console.WriteLine("Invalid Input. Try Again.");
  36.                     }
  37.                     break;
  38.  
  39.                 //checking the max day value of the chosen month and alarm or initialise
  40.                 case 2:
  41.                     Console.WriteLine("Enter your day of birth here /ex. 13/: ");
  42.                     check = int.TryParse(Console.ReadLine(), out day);
  43.                     if (!check || day < 0 || day > DateTime.DaysInMonth(year, month))
  44.                     {
  45.                         cnt--;
  46.                         Console.Clear();
  47.                         int maxDays = DateTime.DaysInMonth(year, month);
  48.                         Console.WriteLine(day > maxDays ? "Invalid Input. This Month Only Has {0} days." :
  49.                  "Invalid Input. Try Again.", maxDays);
  50.                     }
  51.                     break;
  52.                 default:
  53.                     Console.WriteLine("Something went wrong here.");
  54.                     break;
  55.             }
  56.         }
  57.         //The date of birth from the u.input
  58.         DateTime birthday = new DateTime(year, month, day);
  59.         int age = today.Year - birthday.Year;
  60.  
  61.         /*check if the value of age added to the birthday
  62.          date gives result bigger than today's date and correct
  63.          the result according to the month and day of birth and current ones. */
  64.         if (today < birthday.AddYears(age))
  65.         {
  66.             age--;
  67.         }
  68.         //display the result and hold the console open
  69.         Console.WriteLine("Your age is {0} and after ten years, you'll be {1}.", age, age + 10);
  70.         Console.ReadLine();
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement