码迷,mamicode.com
首页 > 其他好文 > 详细

控制台日历代码编写

时间:2017-10-01 19:30:19      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:break   oss   date   list   回车   日历   控制   ssd   ons   

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;
}
}
}
List<string> dates = new List<string>();
int crossDaysOfYear = 0;
for (int i = 1900; i < year; i++)
{
if (i % 4 == 0 && 1 % 100 != 0 || i % 400 == 0)
{
crossDaysOfYear += 366;
}
else
{
crossDaysOfYear += 365;
}
}
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++)
{
dates.Add("");
}
int days;
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++)
{
dates.Add(i.ToString());
}
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.ReadLine();

控制台日历代码编写

标签:break   oss   date   list   回车   日历   控制   ssd   ons   

原文地址:http://www.cnblogs.com/tydy9891-/p/7617599.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!