已知道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();
}
}
}
}

C# 控制台日历 region分区编写思想的更多相关文章

  1. C# 哥德巴赫猜想的实现方式 region分区编写

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  2. 【转帖】HBase之五:hbase的region分区

    HBase之五:hbase的region分区 https://www.cnblogs.com/duanxz/p/3154487.html 一.Region 概念 Region是表获取和分布的基本元素, ...

  3. 如何用C#完成控制台日历?

    本题目的最终要就是根据用户输入的年和月在控制台输出单月的日历信息,附加范围年在1900-2100之间,月的范围在1-12之间,当用户输入不在范围时要给予错误信息提示:已知条件是1900年1月1日为星期 ...

  4. HBase之五:hbase的region分区

    一.Region 概念 Region是表获取和分布的基本元素,由每个列族的一个Store组成.对象层级图如下: Table (HBase table) Region (Regions for the ...

  5. Java Calendar实现控制台日历

    public static void main(String[] args) throws IOException { //初始化日历对象 Calendar calendar = Calendar.g ...

  6. 用最笨的方法实现java控制台日历打印

    如果想用户自定义输入日期查询,可以通过Calendar的set方法和Scanner方法设置 Calendar类简单使用:https://blog.csdn.net/weixin_43670802/ar ...

  7. gtest测试代码编写思想

    mockXXX类 . testXXX类 . mock method 1. mockXXX 类,通常使用继承测试目标类的方法,来方便针对目标类的测试提供部分扩展功能,比如为protected 成员添加g ...

  8. VS2017创建控制台应用后,编写完代码调试正常,使用exe文件直接执行出现闪退情况解决方法。

    这是因为代码中包含的相对路径的原因. 解决办法:把项目中包含的所有相对路径修改为绝对路径. (个人觉得因为直接执行exe文件,默认打开在C盘的用户目录下.) 例如: std::string DATA_ ...

  9. .NET CORE与Spring Boot编写控制台程序应有的优雅姿势

    本文分别说明.NET CORE与Spring Boot 编写控制台程序应有的“正确”方法,以便.NET程序员.JAVA程序员可以相互学习与加深了解,注意本文只介绍用法,不会刻意强调哪种语言或哪种框架写 ...

随机推荐

  1. 数据库连接池Flask-SQLAlchemy中多线程安全的问题

    使用flask-sqlalchemy写代码码到一半,突然想到,Session是否是线程安全的?于是上官方文档,答案是否! 那问题来了,怎么破?因为它会牵涉到多线程情况下,调用rollback导致的不可 ...

  2. osgOcean编译

    E:\Visual Studio 2015\install\VC>e: E:\Visual Studio 2015\install\VC>E:\Visual Studio 2015\ins ...

  3. MySQL数据库之多线程备份工具mydumper

    Mydumper介绍: 1)Mydumper是一个针对MySQL和Drizzle的高性能多线程备份和恢复工具 2)特性: 轻量级C语言编写 执行速度比mysqldump快10倍 快速的文件压缩 支持导 ...

  4. [Scikit-learn] 1.1 Generalized Linear Models - from Linear Regression to L1&L2

    Introduction 一.Scikit-learning 广义线性模型 From: http://sklearn.lzjqsdd.com/modules/linear_model.html#ord ...

  5. Linux——定时任务crontab

    linux内置的cron进程能帮我们实现这些需求,cron搭配shell脚本,非常复杂的指令也没有问题. cron介绍 我们经常使用的是crontab命令是cron table的简写,它是cron的配 ...

  6. Windows服务操作帮助类

    /// <summary> /// 打开系统服务 /// </summary> /// <param name="serviceName">系统 ...

  7. Machine Learning Stanford Univerisity (Week 1)

    1. 机器学习是什么? "A computer program is said to learn from experience E with respect to some class o ...

  8. Oracle 数据库 alert日志及trace日志的清理

    Oracle 数据库 alert日志及trace日志的清理 方案一: 暂停数据库的trace 登录到数据库 sqlplus / as sysdba 修改参数: SQL> alter system ...

  9. 乐字节Java反射之四:反射相关操作

    大家好,乐字节小乐继续为Java初学者讲述Java基础知识.上次说到乐字节Java反射之三:方法.数组.类加载器,这次是Java反射之四:反射相关操作 1.操作属性 //1.获取Class对象 Cla ...

  10. Detect cycle in a directed graph

    Question: Detect cycle in a directed graph Answer: Depth First Traversal can be used to detect cycle ...