//请用户输年份,再输入月份,输出该月的天数.(结合之前判断闰年来做)
Console.WriteLine("请输入一个年份");
try
{
int year = Convert.ToInt32(Console.ReadLine()); //年份
Console.WriteLine("请输入一个月份");
try
{
int month = Convert.ToInt32(Console.ReadLine()); //月份1-12
int day = 0; //声明一个day变量,用于存储天数
if(month>=1&&month<=12)
{
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;
//2 4 6 9 11
default: day =30;
break;
}
Console.WriteLine("{0}年{1}月有{2}天“,year,month,day);
}
else
{
Console.WriteLine("输入的月份有误,程序退出!");
}
}
catch //月份输入try配对
{
Console.WriteLine("输入的月份错误,程序退出!");
}
}
catch // 跟年份输入的try配对
{
Console.WriteLine("年份输入错误,程序退出!");
}
Console.ReadKey();