代码实现:根据输入的年份,打印该年的年历


输入错误提示部分代码省略,只考虑年份输入正确的情况!

代码如下:

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


namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("请输入一个年份:");
            int year = int.Parse(Console.ReadLine());
            //计算1900 — year-1年经过的天数


            for (int month = 1; month <= 12; month++)
            {


                List<string> date = new List<string>();
                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("");
                }


                #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(month + "月");
                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("==============================================================");
            }
                #endregion


        }
    }
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值