首页 > 其他 > 详细

控制台日历

时间:2019-10-05 23:28:53      阅读:162      评论:0      收藏:0      [点我收藏+]

已知道1900-1-1为星期一。

模块分区

//获取用户的正确输入并分别保存到变量year和month中

//声明一个用于保存空白和当月日期数的集合dates

//遍历输出集合dates

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
while (true)
{
#region 获取用户输入的年月,保存到变量year,month,死循环
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.WriteLine("输入月份错误,请按回车键继续");
Console.ReadLine();
Console.Clear();
}
else
{
break;
}
}
}
#endregion
#region 集合dates 依次保存空白和每月的天数date
List<string> dates = new List<string> { };
#region 求空白的数量,保存到变量space中,并保存到集合dates中
//已知1900-1-1是星期1,求year-month-1与其的关系
//求space=(year-month-1)的星期几-1。space= dayOfWeek-1
//求dayOfWeek,为与1900-1-1隔多少天后,设为变量crossDays,dayOfWeek=crossDays%7+1
//求crossDays,从1900-1-1到year-month-1经过了多少天,为1900到year-1经过的总天数,加,从year年中的1月1日到month月1日的天数

//先求从1900-1-1,到year-month-1经过的天数
int space, dayOfWeek, crossDays, crossDaysOfYear = 0, crossDaysOfMonth = 0;
for (int i = 1990; i <= year - 1; i++)
{
//循环变量i为某一年
if ((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0))//闰年
{
crossDaysOfYear += 366;
}
else
{
crossDaysOfYear += 365;
}
}
for (int i = 1; i <= month-1; i++)
{
//循环变量i为某月
if (i == 2)
{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
crossDaysOfMonth += 29;
}
else
{
crossDaysOfMonth += 28;
}
}
else if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12)
{
crossDaysOfMonth += 31;
}
else
{
crossDaysOfMonth += 30;
}
}
crossDays = crossDaysOfYear + crossDaysOfMonth;//相隔的总天数
dayOfWeek = crossDays % 7 + 1;
space = dayOfWeek - 1;
for (int i = 0; i < space; i++)//添加空白字符串到集合中去
{
dates.Add("");
}
#endregion
#region 当月的天数date,再添加到集合中去
int days;//用户输入month的当月天数
if (month == 2)
{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
days = 29;
}
else
{
days = 28;
}
}
else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
days = 31;
}
else
{
days = 30;
}
for (int i = 1; i <= days; i++)
{
dates.Add(i.ToString());
}
#endregion
#endregion
#region 遍历输出集合dates
Console.WriteLine("===================================");
Console.Write("一\t二\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("===================================");
#endregion
Console.Write("按回车键继续");
Console.ReadLine();
Console.Clear();
}
}
}
}

控制台日历

原文:https://www.cnblogs.com/yt0817/p/11625973.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!