Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class AgeNowAndAfter
- {
- static void Main(string[] args)
- {
- int year = 0, month = 0, day = 0;
- //get the current date values
- DateTime today = DateTime.Today;
- //initialize the birthday values in the corresponding inteher variables
- for (int cnt = 0; cnt < 3; cnt++)
- {
- bool check = false;
- switch (cnt)
- {
- case 0:
- Console.WriteLine("Enter your year of birth here /ex. 2014/: ");
- check = int.TryParse(Console.ReadLine(), out year);
- if (!check || year < 1900 || year > today.Year)
- {
- cnt--;
- Console.Clear();
- Console.WriteLine("Invalid Input. Try Again.");
- }
- break;
- case 1:
- Console.WriteLine("Enter your month of birth here /ex. 9/: ");
- check = int.TryParse(Console.ReadLine(), out month);
- if (!check || month < 1 || month > 12)
- {
- cnt--;
- Console.Clear();
- Console.WriteLine("Invalid Input. Try Again.");
- }
- break;
- //checking the max day value of the chosen month and alarm or initialise
- case 2:
- Console.WriteLine("Enter your day of birth here /ex. 13/: ");
- check = int.TryParse(Console.ReadLine(), out day);
- if (!check || day < 0 || day > DateTime.DaysInMonth(year, month))
- {
- cnt--;
- Console.Clear();
- int maxDays = DateTime.DaysInMonth(year, month);
- Console.WriteLine(day > maxDays ? "Invalid Input. This Month Only Has {0} days." :
- "Invalid Input. Try Again.", maxDays);
- }
- break;
- default:
- Console.WriteLine("Something went wrong here.");
- break;
- }
- }
- //The date of birth from the u.input
- DateTime birthday = new DateTime(year, month, day);
- int age = today.Year - birthday.Year;
- /*check if the value of age added to the birthday
- date gives result bigger than today's date and correct
- the result according to the month and day of birth and current ones. */
- if (today < birthday.AddYears(age))
- {
- age--;
- }
- //display the result and hold the console open
- Console.WriteLine("Your age is {0} and after ten years, you'll be {1}.", age, age + 10);
- Console.ReadLine();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement