导航

判断某年某月有几天

Posted on 2016-03-27 18:50  颍川小哥  阅读(257)  评论(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();