首页 > 代码库 > 闰年与月份判断
闰年与月份判断
Console.WriteLine("您想知道哪年");
try {
int year = Convert.ToInt32(Console.ReadLine());
try {
Console.WriteLine("请输入月份");
int month = Convert.ToInt32(Console.ReadLine());
if (month >= 1 && month <= 12)
{
int day = 0;
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: day = 31;
break;
case 2:
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
{
day = 29;
}
else
{
day = 28;
}
break;
default: day = 30;
break;
}
Console.WriteLine("{0}年{1}月有{2}天", year, month, day);
}//判断月份是否输入错的括号
else {
Console.WriteLine("您输入的月份有误");
}
}//try月份的括号
catch
{
Console.WriteLine("您输入的月份有误,程序退出");
}
}
catch
{
Console.WriteLine("您输入的年份有误,程序退出");
}
Console.ReadKey();
闰年与月份判断