try
{
int year = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入月份:");
try
{
int month = Convert.ToInt32(Console.ReadLine());
if (month >= 1 && month <= 12)
{
int day = 0;
switch (month)
{
case 1: //一到十二月中31天的月份天数都一样,可以把case冒号后面的内容省略到最后一个再写,代表前面的和最后一个一样
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; //其他月份都是30天。用default来表示
break;
}
Console.WriteLine("{0}年{1}月有{3}天", year, month, day);
}//if的括号
else
{
Console.WriteLine("请输入正确的月份");
}
}//try月份的括号
catch
{
Console.WriteLine("您的月份有误");
}
}//try年份的括号
catch
{
Console.WriteLine("您输入的年份有误");
}
Console.ReadKey();
Console.WriteLine("{0}年{1}月有{3}天", year, month, day);
参数的顺序也不对0,1,2 . 改成下面这样 ==>
Console.WriteLine(string.Format("{0}年{1}月有{2}天", year, month, day));
这一句有很大问题。Console.WriteLine没有能像你这样输出的。你得用String.Format构造输出串