Ewe kean tan
asked on
C sharp.. Need to ask user input month n day read you horoscopes
class Program
{
public static void CalculateAge(int y, int m, int d)
{
try
{
DateTime date = Convert.ToDateTime(y + "-" + m + "-" + d);
var bday = float.Parse(date.ToString("yyyy.MMdd"));
var now = float.Parse(DateTime.Now.ToString("yyyy.MMdd"));
float age = (now - bday);
if (now <= bday || age >= 135)
{
Console.WriteLine("Sorry, this is an Invalid Input of date.");
Console.ReadLine();
}
else
{
Console.WriteLine("Your Age is " + (age.ToString().Substring(0, age.ToString().LastIndexOf('.'))));
Console.ReadLine();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadLine();
}
}
static void Main(string[] args)
{
var day = "";
var month = "";
var year = "";
int iDay, iMonth, iYear;
Console.WriteLine("Hello, Welcome to Birthday Calculator!");
Console.WriteLine("Please Enter your Birth Day: ");
day = Console.ReadLine();
while (!int.TryParse(day, out iDay))
{
Console.WriteLine("Enter valid numeric day.");
day = Console.ReadLine();
}
Console.WriteLine("Please Enter your Birth Month: ");
month = Console.ReadLine();
while (!int.TryParse(month, out iMonth))
{
Console.WriteLine("Enter valid numeric month.");
month = Console.ReadLine();
}
Console.WriteLine("Please Enter your Birth Year: ");
year = Console.ReadLine();
while (!int.TryParse(year, out iYear))
{
Console.WriteLine("Enter valid numeric year.");
year = Console.ReadLine();
}
CalculateAge(iYear, iMonth, iDay);
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Please change your CalculateAge method as shown below:
public static void CalculateAge(int y, int m, int d)
{
try
{
DateTime bday = Convert.ToDateTime(y + "-" + m + "-" + d);
var now = DateTime.Now;
int age = now.Year - bday.Year;
if (bday > now.AddYears(-age)) age--;//minor correction for month.
if (now <= bday || age >= 135)
{
Console.WriteLine("Sorry, this is an Invalid Input of date.");
Console.ReadLine();
}
else
{
Console.WriteLine("Your Age is " + age.ToString());
Console.ReadLine();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadLine();
}
}
ASKER
Guy .. i want calculate the age of user and the astrology sign ...
how to calculate the astrology sign ??
how to calculate the astrology sign ??
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp5
{
class Program
{
public static void CalculateHoroscope(DateTime date)
{
string[] Sign = new string[] { "Capricorn", "Aquarius", "Pisces", "Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", "Scorpio", "Sagitariius" };
int DayOfYear = date.DayOfYear;
var YourSign = "";
DateTime tdate;
int leapyear = Convert.ToInt32(DateTime.IsLeapYear(date.Year));
var isleepyear = leapyear.ToString();
tdate = DateTime.Parse("1900/03/21");
int Aries = tdate.DayOfYear+ leapyear;
tdate = DateTime.Parse("1900/04/20");
int Taurus = tdate.DayOfYear + leapyear;
tdate = DateTime.Parse("1900/05/21");
int Gemini = tdate.DayOfYear + leapyear;
tdate = DateTime.Parse("1900/06/21");
int Cancer = tdate.DayOfYear + leapyear;
tdate = DateTime.Parse("1900/07/23");
int Leo = tdate.DayOfYear + leapyear;
tdate = DateTime.Parse("1900/07/23");
int Virgo = tdate.DayOfYear + leapyear;
tdate = DateTime.Parse("1900/10/23");
int Libra = tdate.DayOfYear + leapyear;
int Scorpio = (DateTime.Parse("1900/10/23")).DayOfYear + leapyear;
int Sagitarius = (DateTime.Parse("1900/11/23")).DayOfYear + leapyear;
int Capricorn = (DateTime.Parse("1900/12/22")).DayOfYear + leapyear;
int Aquarius = (DateTime.Parse("1900/01/20")).DayOfYear;
int Pisces =(DateTime.Parse("1900/02/19")).DayOfYear;
if (DayOfYear >= Capricorn || DayOfYear < 20)
{
YourSign = Sign[0];
}
else if (DayOfYear >= Aquarius && DayOfYear < Pisces)
{
YourSign = Sign[1];
}
else if (DayOfYear >= Pisces && DayOfYear < Aries)
{
YourSign = Sign[2];
}
else if (DayOfYear >= Aries && DayOfYear < Taurus)
{
YourSign = Sign[3];
}
else if (DayOfYear >= Taurus && DayOfYear < Gemini)
{
YourSign = Sign[4];
}
else if (DayOfYear >= Gemini && DayOfYear < Cancer)
{
YourSign = Sign[5];
}
else if (DayOfYear >= Cancer && DayOfYear < Leo)
{
YourSign = Sign[6];
}
else if (DayOfYear >= Leo && DayOfYear < Virgo)
{
YourSign = Sign[7];
}
else if (DayOfYear >= Virgo && DayOfYear < Scorpio)
{
YourSign = Sign[8];
}
else if (DayOfYear >= Libra && DayOfYear < Scorpio)
{
YourSign = Sign[9];
}
else if (DayOfYear >= Scorpio && DayOfYear < Sagitarius)
{
YourSign = Sign[10];
}
else if (DayOfYear >= Sagitarius)
{
YourSign = Sign[11];
}
Console.Write("Your Zodiac Sign is:");
Console.WriteLine(YourSign);
}
public static void CalculateAge(DateTime date)
{
try
{
DateTime now = DateTime.Today;
var byear = date.Year;
var currentyear = now.Year;
var age = currentyear - byear;
if (now <= date || age >= 135)
{
Console.WriteLine("Sorry, this is an Invalid Input of date.");
Console.ReadLine();
}
else
{
Console.WriteLine("Your Age is " + age.ToString());
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadLine();
}
}
static void Main(string[] args)
{
var day = "";
var month = "";
var year = "";
int iDay, iMonth, iYear;
DateTime Date;
Console.WriteLine("Hello, Welcome to Birthday Calculator!");
Console.Write("Please Enter your Birth Day: ");
day = Console.ReadLine();
while (!int.TryParse(day, out iDay))
{
Console.WriteLine("Enter valid numeric day.");
day = Console.ReadLine();
}
Console.Write("Please Enter your Birth Month: ");
month = Console.ReadLine();
while (!int.TryParse(month, out iMonth))
{
Console.WriteLine("Enter valid numeric month.");
month = Console.ReadLine();
}
Console.Write("Please Enter your Birth Year: ");
year = Console.ReadLine();
while (!int.TryParse(year, out iYear))
{
Console.WriteLine("Enter valid numeric year.");
year = Console.ReadLine();
}
var idate = year + "/" + month + "/" + day;
if (DateTime.TryParse(idate, out Date))
{
CalculateAge(Date);
CalculateHoroscope(Date);
}
else
{
Console.WriteLine("Invalid Date");
}
Console.ReadLine();
}
}
}
ASKER