using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 年份判断
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入年份");
int year = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入月份");
int mounth = Convert.ToInt32(Console.ReadLine());
int day = 0;
switch (mounth)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: day = 31;
break;
case 4:
case 6:
case 11: day = 30;
break;
case 2:
if (year % 400 == 0 || (year % 4 == 0 && (year % 100 != 0)))
{
day = 29;
}
else
{
day = 28;
}
break;
default:
break;
}
Console.WriteLine("{0}年的{1}月有{2}天", year, mounth, day);
}
}
}
try catch 异常处理后的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 年份判断
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("请输入年份");
int year = Convert.ToInt32(Console.ReadLine());
try
{
Console.WriteLine("请输入月份");
int mounth = Convert.ToInt32(Console.ReadLine());
int day = 0;
switch (mounth)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
case 4:
case 6:
case 11:
day = 30;
break;
case 2:
if (year % 400 == 0 || (year % 4 == 0 && (year % 100 != 0)))
{
day = 29;
}
else
{
day = 28;
}
break;
default:
break;
}
Console.WriteLine("{0}年的{1}月有{2}天", year, mounth, day);
}
catch
{
Console.WriteLine("输入的月份有误");
}
}
catch//跟年份的try配对
{
Console.WriteLine("输入年份有误,请重新输入");
}
//从全局的角度考虑异常
}
}
}