c#打印日历

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

namespace 编写日历
{
    class Program
    {
        //判断一年是否为闰年
        static private bool IsLeapYear(int year)
        {
            return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
        }
        //一个月的天数
        static private int GetDayByMonth(int year,int month)
        {
            if (month < 1 || month > 12)
                return 0;
            switch (month)
            {
                case 2:
                    return IsLeapYear(year) ? 29 : 28;
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    return 31;
                default:
                    return 30;
            }

        }
        //判断一年中月份的天数是在星期几
        static private int GetWeekByDay(int year,int month,int day)
    {
        DateTime dt=new DateTime(year,month,day);
        return (int)dt.DayOfWeek;
    }
        //打印月历
        static private void PrintMonthCalender(int year,int month)
        {
            Console.WriteLine("{0}年{1}月", year, month);
            Console.WriteLine("日\t一\t二\t三\t四\t五\t六\t");
            int week=GetWeekByDay(year,month,1);
            for(int i=0;i<week;i++)
            {
                Console.Write("\t");
            }
            int days = GetDayByMonth(year, month);
            for(int i=1;i<=days;i++)
            {
                Console.Write(i + "\t");
                if (GetWeekByDay(year, month, i) == 6)
                Console.WriteLine();
            }

        }
        //打印年历
        static private void PrintYearCalender(int year)
        {
            for(int i=1;i<=12;i++)
            {
                PrintMonthCalender(year,i);
                Console.WriteLine();
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine("请输入年份:");
            int year = int.Parse(Console.ReadLine());
            PrintYearCalender(year);
            Console.ReadLine();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值