3月8日 Switch case

本文提供了一个简单的C#控制台应用程序示例,用于验证用户输入的日期是否有效。通过一系列条件判断来确定年、月、日是否符合标准公历规定,包括闰年的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//输入年月日,判断是否正确
            Console.WriteLine("输入一个年份");
            int year = int.Parse(Console.ReadLine());
            Console.WriteLine("输入一个月份");
            int month = int.Parse(Console.ReadLine());
            Console.WriteLine("输入一个日期");
            int date = int.Parse(Console.ReadLine());
            if (year >= 0 && year <= 9999)
            {
                Console.WriteLine(year+"");
                if (month >= 1 && month <= 12)
                {
                    Console.WriteLine(month+"");
                    if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
                    {
                        if(date>=1&&date<=31)
                    {
                        Console.WriteLine(+date+"");
                    }
                        else
                        {
                            Console.WriteLine("您输入的日期有误");
                        }
                    }
                    else if(month==4||month==6||month==9||month==11)
                    {
                        if(date>=1&&date<=30)
                        {
                            Console.WriteLine(+date+"");
                        }
                        else
                        {
                            Console.WriteLine("您输入的日期有误");
                        }
                    }
                    else
                    {
                        if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                    {
                            if(date>=1&&date<=29)
                            {
                                Console.WriteLine(+date+"");
                            }
                            else
                            {
                                Console.WriteLine("您输入的日期有误");
                            }
                    }
                        else
                        {
                            if(date>=1&&date<=28)
                        {
                                Console.WriteLine(+date+"");
                            }
                            else
                            {
                                Console.WriteLine("您输入的日期有误");
                            }
                        }
                        

                    }

                    
                            
                    }
                 else
                {
                    Console.WriteLine("您输入的月份有误");
                }
            }

               
        else
    {
                                Console.WriteLine("您输入的年份有误");
    
    }
            Console.ReadLine();

 

转载于:https://www.cnblogs.com/dongqiaozhi/p/5253751.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值