输入年月日, 判断输入的是否正确

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

namespace 条件语句日期练习
{
    class Program
    {
        static void Main(string[] args)
        {
            //用户输入年、月、日,判断用户输入的年月日是否正确

            //用户输入年份
            Console.Write("请输入年份(0-9999):");
            int y = Convert.ToInt32(Console.ReadLine());

            //判断年份是否正确,0-9999
            if (y >= 0 && y <= 9999)//年份正确
            {
                //用户输入月份
                Console.Write("请输入月份(1-12):");
                int m = Convert.ToInt32(Console.ReadLine());

                //判断月份是否正确,1-12
                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.特殊

                    //如果此条件成立,那么说明是大月
                    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 + "");
                        }
                        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 + "");
                        }
                        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 + "年是闰年!");
                            }
                            else//日期错误
                            {
                                Console.WriteLine("日期输入有误!");
                            }
                        }
                        else//说明不是闰年
                        {
                            if (d <= 28 && d > 0)//日期正确
                            {
                                Console.WriteLine("日期输入正确!");
                                Console.WriteLine(y + "" + m + "" + d + "");
                                Console.WriteLine(y + "年不是闰年!");
                            }
                            else//日期错误
                            {
                                Console.WriteLine("日期输入有误!");
                            }
                        }
                    }
                }
                else//月份错误
                {
                    Console.WriteLine("月份输入有误!");
                }
            }
            else//年份输入错误
            {
                Console.WriteLine("年份输入有误!");
            }

            Console.ReadLine();
        }
    }
}
原文地址:https://www.cnblogs.com/tonyhere/p/5469784.html