C#制作简易日历

 while (true)
            {
                #region 输入年月
                int year, month;
                while (true)
                {
                    Console.WriteLine("请输入年份1900-2100");
                    year = int.Parse(Console.ReadLine());
                    if (year > 2100 || year < 1900)
                    {
                        Console.WriteLine("输入有误,请重新输入");
                        Console.ReadLine();
                        Console.Clear();
                    }
                    else
                    {
                        Console.WriteLine("请输入月份");
                        month = int.Parse(Console.ReadLine());
                        if (month < 1 || month > 12)
                        {
                            Console.WriteLine("输入有误,请重新输入");
                            Console.ReadLine();
                            Console.Clear();
                        }
                        else
                        {
                            break;
                        }
                    }

                }
                #endregion
                #region 获得该年该月所有的日期包括前面的空白,并保存在集合date中
                List<string> date = new List<string>();
                //计算1900 — year-1年经过的天数
                int crossDaysOfYear = 0;
                for (int i = 1900; i < year; i++)
                {
                    if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
                    {
                        crossDaysOfYear += 366;
                    }
                    else
                    {
                        crossDaysOfYear += 365;
                    }
                }
                //在year这一年从1月到month-1月经过的天数
                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++)
                {
                    date.Add("");
                }
                #endregion
                #region  将日期数字加入到集合
                int days;//用户输入month这个月的天数
                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++)
                {
                    date.Add(i.ToString());
                }
                #endregion
                #region 输出结果
                Console.WriteLine("==============================================================");
                Console.Write("一\t二\t三\t四\t五\t六\t日");
                for (int i = 0; i < date.Count; i++)
                {
                    if (i % 7 == 0)
                    { Console.WriteLine(); }
                    Console.Write(date[i] + "\t");
                }
                Console.WriteLine();
                Console.WriteLine("==============================================================");
                Console.ReadLine();
                Console.Clear();
                #endregion
            }

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值