zoukankan      html  css  js  c++  java
  • 如何用C#完成控制台日历?

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

    要输出此日历就需要知道该月的第一天是星期几,确定后才好根据天数推出后面的日期。其具体实现的代码如下:

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

    namespace ConsoleApp18
    {
    class Program
    {
    static void Main(string[] args)
    {
    while (true)
    {
    int year, month;
    List<string> dates = new List<string>();
    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;
    }
    }
    }
    int dayYear = 0;//计算1900到year-1年经过的天数
    for (int i = 1900; i < year; i++)
    {
    if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
    {
    dayYear += 366;
    }
    else
    {
    dayYear += 365;
    }
    }
    int dayMonth = 0;//计算在变量year这一年,从1月到month-1的天数
    for (int i = 1; i < month; i++)
    {
    if (i == 2)
    {
    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
    {
    dayMonth += 29;
    }
    else
    {
    dayMonth += 28;
    }
    }
    else if (i <= 7 && i % 2 == 1 || i >= 8 && i % 2 == 0)
    {
    dayMonth += 31;
    }
    else
    {
    dayMonth += 30;
    }
    }
    int allDays = dayYear + dayMonth;//经过的总天数
    int dayWeek = allDays % 7 + 1;//year-month-1是星期几
    int space = dayWeek - 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 == 1 || month >= 8 && 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("\n");
    }
    Console.Write(dates[i] + "\t");
    }
    Console.WriteLine();
    Console.WriteLine("*******************************************************");
    Console.Write("按回车键后继续");
    Console.ReadLine();
    Console.Clear();
    }
    }
    }
    }

    效果图:

    此题的难点在于根据用户输入的年月计算出与已知1900年1月1日所在的星期一相隔的天数,根据天数计算出单月的第一天是星期几,如果一星期一作为日历的第一个输出点后面类推,就需要知道该月第一天所在的星期与星期一相隔多少个空格,当第一天确定后,后面的日期就好判断了。
  • 相关阅读:
    201571030134 由《构建之法》所想到的~
    unity3d 打包Xcode工程自动添加framework
    Unity3d Vuforia与Mobile Movie Texture插件结合
    Unity3d 打开本地摄像头扫描二维码
    Unity3d 获取文件夹的图片
    iOS开发 Button自定义
    Unity3D判断鼠标左右滑动
    unity3d控制物体旋转的角度
    第一次作业
    人工智能第二次作业
  • 原文地址:https://www.cnblogs.com/gassnake999/p/6854369.html
Copyright © 2011-2022 走看看