输入错误提示部分代码省略,只考虑年份输入正确的情况!
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Console.Write("请输入一个年份:");
int year = int.Parse(Console.ReadLine());
//计算1900 — year-1年经过的天数
for (int month = 1; month <= 12; month++)
{
List<string> date = new List<string>();
int crossDaysOfYear = 0;
for (int i = 1900; i < year; i++)
{
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
{
crossDaysOfYear += 366;
}
else
{
crossDaysOfYear += 365;
}
}
//在year这一年从1月到month-1月经过的天数
int crossDaysOfMonth = 0;
for (int i = 1; i < month; i++)
{
if (i == 2)
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
crossDaysOfMonth += 29;
}
else
{
crossDaysOfMonth += 28;
}
}
else if (i <= 7 && i % 2 != 0 || i > 7 && i % 2 == 0)
{
crossDaysOfMonth += 31;
}
else
{
crossDaysOfMonth += 30;
}
}
int crossDays = crossDaysOfYear + crossDaysOfMonth;
int dayOfWeek = crossDays % 7 + 1;
int space = dayOfWeek - 1;
for (int i = 0; i < space; i++)
{
date.Add("");
}
#region 将日期数字加入到集合
int days;//用户输入month这个月的天数
if (month == 2)
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
days = 29;
}
else
{
days = 28;
}
}
else if (month <= 7 && month % 2 != 0 || month > 7 && month % 2 == 0)
{
days = 31;
}
else
{
days = 30;
}
for (int i = 1; i <= days; i++)
{
date.Add(i.ToString());
}
#endregion
#region 输出结果
Console.WriteLine(month + "月");
Console.WriteLine("==============================================================");
Console.Write("一\t二\t三\t四\t五\t六\t日");
for (int i = 0; i < date.Count; i++)
{
if (i % 7 == 0)
{
Console.WriteLine();
}
Console.Write(date[i] + "\t");
}
Console.WriteLine();
Console.WriteLine("==============================================================");
}
#endregion
}
}
}