(1)源代码:
using System;
namespace week8_experiment
{
enum MonthName
{
January, February, March, April, May, June, July, August, September, October, November, December
}
class Program
{
static int[] DaysInMonths = new int[12]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static void Main(string[] args)
{
try
{
Boolean leap = true;
Console.WriteLine("请输入索要判断的年份");
String yearNum = Console.ReadLine();
int year = int.Parse(yearNum);
if ((year%4==0 && year%100!=0) || (year % 400 == 0))
{
Console.WriteLine("请输入一个在1-366之间的天数:");
leap = true;
}
else
{
Console.WriteLine("请输入一个在1-365之间的天数:");
leap = false;
}
String line = Console.ReadLine();
int dayNum = int.Parse(line);
if (leap)
{
if (dayNum < 1 || dayNum > 366)
{
throw new ArgumentOutOfRangeException("输入的数据超出范围");
}
}
else
{
if (dayNum < 1 || dayNum > 365)
{
throw new ArgumentOutOfRangeException("输入的数据超出闰年的范围");
}
}
int monthNum = 0;
foreach(int daysInMonth in DaysInMonths)
{
if(dayNum <= daysInMonth)
{
break;
}
else
{
dayNum -= daysInMonth;
monthNum++;
}
}
MonthName temp = (MonthName)monthNum;
string monthName = Enum.Format(typeof(MonthName), temp, "g");
Console.WriteLine("{0} {1}", dayNum, monthName);
Console.Read();
}
catch(Exception e)
{
Console.WriteLine(e);
}
}
}
}
(2)运行结果: