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

4.29. 平年闰年,大小月练习

时间:2016-05-03 14:16:40      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

Console.Write

 

       int y = Convert.ToInt32(Console.ReadLine());

 

            bool isok = false;       

    int m1 = 31, m2 = 28, m3 = 31, m4 = 30, m5 = 31, m6 = 30, m7 = 31, m8 = 31, m9 = 30, m10 = 31, m11 = 30;

            int sum = 0;                 

if (y >= 0 && y <= 9999)

            {

                              

if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)              {

                    isok = true;

                }

                else

                {

                    isok = false;

                }

 

                              Console.Write("请输入月份(1-12):");

                int m = Convert.ToInt32(Console.ReadLine());

 

                    if (m >= 1 && m <= 12)

                {

                                       Console.Write("请输入日期(1-31):");

                    int d = Convert.ToInt32(Console.ReadLine());

 

                    //判断日期是否正确,先看用户输入的月份是大月还是小月,先排除2月

                    //1.大,3.大,5.大,7.大,8.大,10.大,12.大

                    //4.小,6.小,9.小,11.小

                    //2.特殊

 

                    //1.31,2,28/29,3.31,4.30,5.31,6.30,7.31,8.31,9.30

                    //10.31,11.30

 

                                       if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)

                    {

                        if (d <= 31 && d > 0)                        {

                            Console.WriteLine("日期输入正确!");

                            Console.WriteLine(y + "年" + m + "月" + d + "日");

                            //计算当前日期是当年的第几天

                            if (isok)//说明当前是闰年

                            {

                                m2 = 29;

                                switch (m)

                                {

                                    case 1: sum = d; break;

                                    case 2: sum = m1 + d; break;

                                    case 3: sum = m1 + m2 + d; break;

                                    case 4: sum = m1 + m2 + m3 + d; break;

                                    case 5: sum = m1 + m2 + m3 + m4 + d; break;

                                    case 6: sum = m1 + m2 + m3 + m4 + m5 + d; break;

                                    case 7: sum = m1 + m2 + m3 + m4 + m5 + m6 + d; break;

                                    case 8: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + +d; break;

                                    case 9: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + d; break;

                                    case 10: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + d; break;

                                    case 11: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + d; break;

                                    case 12: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + m11 + d; break;

                                }

                                Console.WriteLine(d + "天是" + y + "的第" + sum + "天");

                            }

                            else//不是闰年

                            {

                                m2 = 28;

                                switch (m)

                                {

                                    case 1: sum = d; break;

                                    case 2: sum = m1 + d; break;

                                    case 3: sum = m1 + m2 + d; break;

                                    case 4: sum = m1 + m2 + m3 + d; break;

                                    case 5: sum = m1 + m2 + m3 + m4 + d; break;

                                    case 6: sum = m1 + m2 + m3 + m4 + m5 + d; break;

                                    case 7: sum = m1 + m2 + m3 + m4 + m5 + m6 + d; break;

                                    case 8: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + +d; break;

                                    case 9: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + d; break;

                                    case 10: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + d; break;

                                    case 11: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + d; break;

                                    case 12: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + m11 + d; break;

                                }

                                Console.WriteLine(d + "天是" + y + "的第" + sum + "天");

                            }

                        }

                        else

                        {

                            Console.WriteLine("日期输入有误!");

                        }

                    }

                    else if (m == 4 || m == 6 || m == 9 || m == 11)//输入的是小月

                    {

                        if (d <= 30 && d > 0)//日期输入正确

                        {

                            Console.WriteLine("日期输入正确!");

                            Console.WriteLine(y + "年" + m + "月" + d + "日");

 

                            //计算当前日期是当年的第几天

                            if (isok)//说明当前是闰年

                            {

                                m2 = 29;

                                switch (m)

                                {

                                    case 1: sum = d; break;

                                    case 2: sum = m1 + d; break;

                                    case 3: sum = m1 + m2 + d; break;

                                    case 4: sum = m1 + m2 + m3 + d; break;

                                    case 5: sum = m1 + m2 + m3 + m4 + d; break;

                                    case 6: sum = m1 + m2 + m3 + m4 + m5 + d; break;

                                    case 7: sum = m1 + m2 + m3 + m4 + m5 + m6 + d; break;

                                    case 8: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + +d; break;

                                    case 9: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + d; break;

                                    case 10: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + d; break;

                                    case 11: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + d; break;

                                    case 12: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + m11 + d; break;

                                }

                                Console.WriteLine(d + "天是" + y + "的第" + sum + "天");

                            }

                            else//不是闰年

                            {

                                m2 = 28;

                                switch (m)

                                {

                                    case 1: sum = d; break;

                                    case 2: sum = m1 + d; break;

                                    case 3: sum = m1 + m2 + d; break;

                                    case 4: sum = m1 + m2 + m3 + d; break;

                                    case 5: sum = m1 + m2 + m3 + m4 + d; break;

                                    case 6: sum = m1 + m2 + m3 + m4 + m5 + d; break;

                                    case 7: sum = m1 + m2 + m3 + m4 + m5 + m6 + d; break;

                                    case 8: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + +d; break;

                                    case 9: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + d; break;

                                    case 10: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + d; break;

                                    case 11: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + d; break;

                                    case 12: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + m11 + d; break;

                                }

                                Console.WriteLine(d + "天是" + y + "的第" + sum + "天");

                            }

                        }

                        else

                        {

                            Console.WriteLine("日期输入有误!");

                        }

                    }

                    else if (m == 2)//如果是2月,那么很麻烦

                    {

                        //判断是否是闰年

                        //年份可以被4整除 并且 不能被100整除

                        //特殊年:或者,年份可以被400整除

                        if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)//说明当前是闰年

                        {

                            if (d <= 29 && d > 0)//日期正确

                            {

                                Console.WriteLine("日期输入正确!");

                                Console.WriteLine(y + "年" + m + "月" + d + "日");

                                Console.WriteLine(y + "年是闰年!");

 

                                m2 = 29;

                                switch (m)

                                {

                                    case 1: sum = d; break;

                                    case 2: sum = m1 + d; break;

                                    case 3: sum = m1 + m2 + d; break;

                                    case 4: sum = m1 + m2 + m3 + d; break;

                                    case 5: sum = m1 + m2 + m3 + m4 + d; break;

                                    case 6: sum = m1 + m2 + m3 + m4 + m5 + d; break;

                                    case 7: sum = m1 + m2 + m3 + m4 + m5 + m6 + d; break;

                                    case 8: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + +d; break;

                                    case 9: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + d; break;

                                    case 10: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + d; break;

                                    case 11: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + d; break;

                                    case 12: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + m11 + d; break;

                                }

                                Console.WriteLine(d + "天是" + y + "的第" + sum + "天");

                            }

                            else//日期错误

                            {

                                Console.WriteLine("日期输入有误!");

                            }

                        }

                        else//说明不是闰年

                        {

                            if (d <= 28 && d > 0)//日期正确

                            {

                                Console.WriteLine("日期输入正确!");

                                Console.WriteLine(y + "年" + m + "月" + d + "日");

                                Console.WriteLine(y + "年不是闰年!");

 

                                m2 = 28;

                                switch (m)

                                {

                                    case 1: sum = d; break;

                                    case 2: sum = m1 + d; break;

                                    case 3: sum = m1 + m2 + d; break;

                                    case 4: sum = m1 + m2 + m3 + d; break;

                                    case 5: sum = m1 + m2 + m3 + m4 + d; break;

                                    case 6: sum = m1 + m2 + m3 + m4 + m5 + d; break;

                                    case 7: sum = m1 + m2 + m3 + m4 + m5 + m6 + d; break;

                                    case 8: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + +d; break;

                                    case 9: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + d; break;

                                    case 10: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + d; break;

                                    case 11: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + d; break;

                                    case 12: sum = m1 + m2 + m3 + m4 + m5 + m6 + m7 + m8 + m9 + m10 + m11 + d; break;

                                }

                                Console.WriteLine(d + "天是" + y + "的第" + sum + "天");

                            }

                            else//日期错误

                            {

                                Console.WriteLine("日期输入有误!");

                            }

                        }

                    }

                }

                else//月份错误

                {

                    Console.WriteLine("月份输入有误!");

                }

            }

            else//年份输入错误

            {

                Console.WriteLine("年份输入有误!");

            }

 

            Console.ReadLine();

        }

    }

}

4.29. 平年闰年,大小月练习

标签:

原文地址:http://www.cnblogs.com/suiyuejinghao123/p/5454579.html

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