C#基础知识_根据输入的年月得到该…

值得注意的是闰年2月份有29天,平年28天,用到了上一篇怎么判断输入年份是平年还是闰年
Main方法中代码
  //判断每年的每个月有多少天,初始化变量
            int year   = 2016;
            int month = 1;
            Console.WriteLine("请输入年份:");
            string strYear = Console.ReadLine();
            //验证年份格式输入是不是正确
            CheckYear(strYear);
            year = Convert.ToInt32(strYear);
            //输入月份
            Console.WriteLine("请输入月份:");
            string   strMonth = Console.ReadLine();
            //验证输入的月份是不是正规的月份,不对,直到输入正确为止
            CheckMonth(strMonth);
            //根据月份不同,判断天数为多少天
            month = Convert.ToInt32(strMonth);
                switch (month)
                {
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                        Console.WriteLine("31天");
                        break;
                    case 2:
                        //判断是不是闰年
                        if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
                        {
                            Console.WriteLine("29天"); 
                        }
                        else
                        {
                            Console.WriteLine("28天");
                        }
                        break;
                    default:
                          Console.WriteLine("30天");
                        break;
                }
            Console.ReadKey();
两个验证格式的函数 
          ///
        /// 验证年份是不是正确格式
        ///
        ///
        public static void CheckYear(string strYear)
        {
            try
            {
                int year = Convert.ToInt32(strYear);
                if (year <= 0)
                {
                    Console.WriteLine("输入年份格式不正确,请重新输入!");
                    strYear = Console.ReadLine();
                    CheckYear(strYear);
                }
            }
            catch (Exception)
            {
                Console.WriteLine("输入年份格式不正确!请重新输入!");
                strYear = Console.ReadLine();
                CheckMonth(strYear);
            }
        }

        ///
        /// 验证月份是不是正确格式
        ///
        ///
        public static void CheckMonth(string strMonth)
        {
            try
            {
                int month = Convert.ToInt32(strMonth);
                if (month > 12 || month <= 0)
                {
                    Console.WriteLine("输入月份格式不正确,请重新输入!");
                    strMonth = Console.ReadLine();
                    CheckMonth(strMonth);
                }
            }
            catch (Exception)
            {
                Console.WriteLine("输入月份格式不正确!请重新输入!");
                strMonth = Console.ReadLine();
                CheckMonth(strMonth);
            }
        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寒冰的暖

谢谢您的赞许投喂~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值