using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
//循环整个日历计算器,用于重复显示固定语句与计算
while (true)
{
#region 第一部分:从用户输入中获取正确的年份和月份
int year, month;
//如年份或月份输入错误,直接结束并进入循环从头开始运行
while (true)
{
Console.Write("请输入年份(1900-2100):");
year = int.Parse(Console.ReadLine());
if (year < 1900 || year > 2100)
{
Console.Write("年份错误,请按回车后重新输入。");
Console.ReadLine();
Console.Clear();
}
else
{
Console.Write("请输入月份(1-12):");
month = int.Parse(Console.ReadLine());
if (month < 1 || month > 12)
{
Console.Write("月份错误,请按回车后重新输入。");
Console.ReadLine();
Console.Clear();
}
else
{
break;
}
}
}
#endregion
#region 第二部分:创建一个集合包括当前月份之前所有的天数及当前月份日历第一排空白数
List<string> dates = new List<string>();
#endregion
#region 第三部分:將当前月份日期数字添加到集合中
int crossDayOfYear = 0;
for (int i = 1900; i < year; i++)
{
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
{
crossDayOfYear += 366;
}
else
{
crossDayOfYear += 365;
}
}
int crossDayOfMonth = 0;
for (int i = 1; i < month; i++)
{
if (i == 2)
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
crossDayOfMonth += 29;
}
else
{
crossDayOfMonth += 28;
}
}
else
{
if (month < 7 && month % 2 != 0 || month > 8 && month % 2 == 0)
{
crossDayOfMonth += 31;
}
else
{
crossDayOfMonth += 30;
}
}
}
/*计算每个月前打印的空白,并加入集合。(从1900至用户指定的月份前的所有天数之和
与7(一周)的余数,即是用户指定的月份日历第一排的空白)*/
int crossDay = crossDayOfYear + crossDayOfMonth;
int space = crossDay % 7;
for (int i = 1; i <= space; i++)
{
dates.Add("");
}
//打印用户输入的当前月份的天数
int days = 0;
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 > 8 && month % 2 == 0)
{
days = 31;
}
else
{
days = 30;
}
}
//将用户输入当前月份的天数加入集合dates
for (int i = 1; i <= days; i++)
{
dates.Add(i.ToString());
}
#endregion
#region 第四部分:输出当前月份结果
Console.WriteLine("********************************************************************");
Console.Write("星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t星期天");
for (int i = 0; i < dates.Count; i++)
{
if (i % 7 == 0)
{
Console.WriteLine();
}
Console.Write(dates[i] + "\t");
}
Console.WriteLine();
Console.WriteLine("********************************************************************");
Console.Write("按回车键后继续");
Console.ReadLine();
Console.Clear();
#endregion
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
//循环整个日历计算器,用于重复显示固定语句与计算
while (true)
{
#region 第一部分:从用户输入中获取正确的年份和月份
int year, month;
//如年份或月份输入错误,直接结束并进入循环从头开始运行
while (true)
{
Console.Write("请输入年份(1900-2100):");
year = int.Parse(Console.ReadLine());
if (year < 1900 || year > 2100)
{
Console.Write("年份错误,请按回车后重新输入。");
Console.ReadLine();
Console.Clear();
}
else
{
Console.Write("请输入月份(1-12):");
month = int.Parse(Console.ReadLine());
if (month < 1 || month > 12)
{
Console.Write("月份错误,请按回车后重新输入。");
Console.ReadLine();
Console.Clear();
}
else
{
break;
}
}
}
#endregion
#region 第二部分:创建一个集合包括当前月份之前所有的天数及当前月份日历第一排空白数
List<string> dates = new List<string>();
#endregion
#region 第三部分:將当前月份日期数字添加到集合中
int crossDayOfYear = 0;
for (int i = 1900; i < year; i++)
{
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
{
crossDayOfYear += 366;
}
else
{
crossDayOfYear += 365;
}
}
int crossDayOfMonth = 0;
for (int i = 1; i < month; i++)
{
if (i == 2)
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
crossDayOfMonth += 29;
}
else
{
crossDayOfMonth += 28;
}
}
else
{
if (month < 7 && month % 2 != 0 || month > 8 && month % 2 == 0)
{
crossDayOfMonth += 31;
}
else
{
crossDayOfMonth += 30;
}
}
}
/*计算每个月前打印的空白,并加入集合。(从1900至用户指定的月份前的所有天数之和
与7(一周)的余数,即是用户指定的月份日历第一排的空白)*/
int crossDay = crossDayOfYear + crossDayOfMonth;
int space = crossDay % 7;
for (int i = 1; i <= space; i++)
{
dates.Add("");
}
//打印用户输入的当前月份的天数
int days = 0;
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 > 8 && month % 2 == 0)
{
days = 31;
}
else
{
days = 30;
}
}
//将用户输入当前月份的天数加入集合dates
for (int i = 1; i <= days; i++)
{
dates.Add(i.ToString());
}
#endregion
#region 第四部分:输出当前月份结果
Console.WriteLine("********************************************************************");
Console.Write("星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t星期天");
for (int i = 0; i < dates.Count; i++)
{
if (i % 7 == 0)
{
Console.WriteLine();
}
Console.Write(dates[i] + "\t");
}
Console.WriteLine();
Console.WriteLine("********************************************************************");
Console.Write("按回车键后继续");
Console.ReadLine();
Console.Clear();
#endregion
}
}
}
}