C#万年历代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

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;
}

相关文档
最新文档