使用csharp编写程序:输入一个生日日期,输出其星座
时间: 2024-01-06 18:07:15 浏览: 90
下面是一个使用C#编写的程序,可以根据输入的生日日期输出对应的星座:
```C#
using System;
namespace Constellation
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入生日日期(格式为 MM/DD):");
string input = Console.ReadLine(); // 读取用户输入的生日日期
// 将生日日期字符串转换为DateTime类型
DateTime birthday;
if (!DateTime.TryParse(input + "/" + DateTime.Now.Year.ToString(), out birthday))
{
Console.WriteLine("生日日期格式不正确!");
return;
}
// 根据生日日期计算星座
string constellation = string.Empty;
if ((birthday.Month == 3 && birthday.Day >= 21) || (birthday.Month == 4 && birthday.Day <= 19))
{
constellation = "白羊座";
}
else if ((birthday.Month == 4 && birthday.Day >= 20) || (birthday.Month == 5 && birthday.Day <= 20))
{
constellation = "金牛座";
}
else if ((birthday.Month == 5 && birthday.Day >= 21) || (birthday.Month == 6 && birthday.Day <= 21))
{
constellation = "双子座";
}
else if ((birthday.Month == 6 && birthday.Day >= 22) || (birthday.Month == 7 && birthday.Day <= 22))
{
constellation = "巨蟹座";
}
else if ((birthday.Month == 7 && birthday.Day >= 23) || (birthday.Month == 8 && birthday.Day <= 22))
{
constellation = "狮子座";
}
else if ((birthday.Month == 8 && birthday.Day >= 23) || (birthday.Month == 9 && birthday.Day <= 22))
{
constellation = "处女座";
}
else if ((birthday.Month == 9 && birthday.Day >= 23) || (birthday.Month == 10 && birthday.Day <= 23))
{
constellation = "天秤座";
}
else if ((birthday.Month == 10 && birthday.Day >= 24) || (birthday.Month == 11 && birthday.Day <= 22))
{
constellation = "天蝎座";
}
else if ((birthday.Month == 11 && birthday.Day >= 23) || (birthday.Month == 12 && birthday.Day <= 21))
{
constellation = "射手座";
}
else if ((birthday.Month == 12 && birthday.Day >= 22) || (birthday.Month == 1 && birthday.Day <= 19))
{
constellation = "摩羯座";
}
else if ((birthday.Month == 1 && birthday.Day >= 20) || (birthday.Month == 2 && birthday.Day <= 18))
{
constellation = "水瓶座";
}
else if ((birthday.Month == 2 && birthday.Day >= 19) || (birthday.Month == 3 && birthday.Day <= 20))
{
constellation = "双鱼座";
}
Console.WriteLine("您的星座是:" + constellation);
}
}
}
```
在上面的程序中,我们先使用`Console.ReadLine()`方法读取用户输入的生日日期,并使用`DateTime.TryParse()`方法将其转换为`DateTime`类型。接着,我们根据生日日期的月份和日期计算对应的星座,并输出结果。注意,这里为了方便计算,我们将生日日期的年份设为当前年份。
阅读全文