9999||year{Console.WriteLine("年份输入错误:");}}w" />
当前位置:文档之家› C#万年历代码

C#万年历代码

static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Green;
int year, month;
do
{
Console.WriteLine("请输入年份:");
year = Int32.Parse(Console.ReadLine());
if (year > 9999 || year < 1000)
{
Console.WriteLine("年份输入错误:");
}
}
while (year > 9999 || year < 1000);
do
{
Console.WriteLine("请输入月份:");
month = Int32.Parse(Console.ReadLine());
if (month > 12 || month < 1)
{
Console.WriteLine("月份输入错误:");
}

}
while (month > 12 || month < 1);
ShowDate(year, month);
}
static void ShowDate(int year, int month)
{
string str = new DateTime(year, month, 1).DayOfWeek.ToString();
int space = 0;
switch (str)
{
case "Monday": space = 0; break;
case "Tuesday": space = 1; break;
case "Wednesday": space = 2; break;
case "Thursday": space = 3; break;
case "Friday": space = 4; break;
case "Saturday": space = 5; break;
case "Sunday": space = 6; break;
default: break;
}
Console.Clear();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("您输入的年月{0}-{1}日历", year, month);
Console.WriteLine("******************************************************");
Console.WriteLine("星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t星期日");
Console.WriteLine("******************************************************");
int tmp = 1;
for (int i = 1; i <= space; i++)
{
Console.Write("\t");
tmp++;
}
for (int j = 1; j <= JiSuanday(year, month); j++)
{
Console.Write("{0,2}\t", j);
if (tmp % 7 == 0)
{
Console.WriteLine();
}
tmp++;
}
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("******************************************************");
}
static int JiSuanday(int year, int month)
{
Console.ForegroundColor = ConsoleColor.White;
int day = 0;
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:

day = 31; break;
case 4:
case 6:
case 9:
case 11:
day = 30; break;
case 2:
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
day = 29;
}
else
{
day = 28;
}
break;
}
return day;
}

相关主题
文本预览
相关文档 最新文档