C#可供查询的月历2.0版本

基于第一个版本[C#可供查询的月历]做了一点小修改,让主函数中定义的变量更少,更简洁(https://blog.csdn.net/weixin_48343145/article/details/123514237?spm=1001.2014.3001.5502)

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

namespace testFunction_1
{
    class Program
    {
        static void Main(string[] args)
        {

            int year = 0;
            int month = 0;

            showMonthCalander(year, month);

            Console.ReadLine();
        }
        private static int getWeekByDay(int year, int month, int day)
        {
            DateTime dt = new DateTime(year, month, day);
            return (int)dt.DayOfWeek;
        }

        static int calculateDaysOfMonth(int year, int month)
        {
            bool leapYear;
            if (year % 4 == 0 && year % 100 != 0)
            {
                leapYear = true;
            }
            else
                leapYear = false;


            if (month == 2)
            {
                if (leapYear)
                {
                    return 29;
                }
                else return 28;
            }

            switch (month)
            {
                case 1: return 31;
                case 3: return 31;
                case 5: return 31;
                case 7: return 31;
                case 8: return 31;
                case 10: return 31;
                case 12: return 31;
                case 11: return 30;
                case 4: return 30;
                case 6: return 30;
                case 9: return 30;
            }

            return 1;

        }

        private static void showMonthCalander(int year, int month)
        {
            int i;
            int dayNum = 1;

            Console.WriteLine("请输入想查询的年份");
            year = int.Parse(Console.ReadLine());
            Console.WriteLine("请输入想查询的月份");
            month = int.Parse(Console.ReadLine());
            
            int firWeekday = getWeekByDay(year, month, 1);
            int DaysOfMonth = calculateDaysOfMonth(year, month);
           
            Console.WriteLine("日\t一\t二\t三\t四\t五\t六");
           
            
            for (i = 0; i < firWeekday; i++)
            {
                Console.Write("\t");
            }
            while (i < 7)
            {

                if (i < 6)
                {
                    Console.Write(dayNum++);
                    Console.Write("\t");
                    i++;
                }
                else
                {
                    Console.Write(dayNum++);
                    Console.WriteLine("\r\n");
                    i = 0;
                }
                if (dayNum > DaysOfMonth)
                {
                    break;
                }
            }

        }

    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值