码迷,mamicode.com
首页 > 其他好文 > 详细

判断某年某月有几天

时间:2016-03-27 19:40:42      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

 Console.WriteLine("请输入年份:");
            try
            {
                int year = Convert.ToInt32(Console.ReadLine());//有可能抛异常
                Console.WriteLine("请输入月份:");
                try
                {
                    int month = Convert.ToInt32(Console.ReadLine());//1-12 //有可能抛异常
                    if (month >= 1 && month <= 12)
                    {
                        int day = 0;
                        switch (month)
                        {
                            case 1:
                            case 3:
                            case 5:
                            case 7:
                            case 8:
                            case 12:
                                day = 31;
                                break;

                            case 2:
                                //判断是否是闰年
                                if ((year % 4 == 0) || (year % 4 == 0 && year % 100 != 0))
                                {
                                    day = 29;
                                }
                                else
                                {
                                    day = 28;
                                }
                                break;

                            default:
                                day = 30;
                                break;
                        }
                        Console.WriteLine("{0}年{1}月有{2}天", year, month, day);

                    }//if判断的括号
                    else
                    {
                        Console.WriteLine("输入的月份不合要求");
                    }
                }//try月份的括号
                catch//跟月份配套
                {
                    Console.WriteLine("输入的月份有误");
                }
            }//try年份的括号
            catch //跟年份配套try
            {
                Console.WriteLine("输入的年份有误");
            }
            Console.ReadKey();

 

判断某年某月有几天

标签:

原文地址:http://www.cnblogs.com/yingchuanxiaoge/p/5326449.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!