• .NET Framework : 4.7.2
  •        IDE : Visual Studio Community 2019
  •         OS : Windows 10 x64
  •     typesetting : Markdown

code

using System;

namespace ConsoleApp
{
    class Program
    {

        static bool IsLeapYear(int year)
        {
            if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
                return true;
            else
                return false;
        }

        static void Main(string[] args)
        {
            // 抓的年份的异常
            try
            {
                Console.WriteLine("请输入年份:");
                int year = Convert.ToInt32(Console.ReadLine());

                // 抓的是月份的异常
                try
                {
                    Console.WriteLine("请输入月份");
                    int month = Convert.ToInt32(Console.ReadLine());

                    switch (month)
                    {
                        // 1357810腊 31天永不差
                        case 1:
                        case 3:
                        case 5:
                        case 7:
                        case 8:
                        case 10:
                        case 12:
                            Console.WriteLine("{0}年{1}月有31天", year, month);
                            break;
                        // 46911 30日
                        case 4:
                        case 6:
                        case 9:
                        case 11:
                            Console.WriteLine("{0}年{1}月有30天", year, month);
                            break;
                        case 2:
                            if (!(IsLeapYear(year)))
                            {
                                // 不是闰年,平年二月二十八
                                Console.WriteLine("{0}年{1}月有28天", year, month);
                            }
                            else
                            {
                                // 是闰年,闰年在把一日加
                                Console.WriteLine("{0}年{1}月有29天", year, month);
                            }
                            break;
                        default:
                            Console.WriteLine("没有这个月");
                            break;
                    }
                }
                catch  // 注意看,这个catch与哪个try是一对的
                {
                    Console.WriteLine("输入的月份有误");
                }
            }
            catch
            {
                Console.WriteLine("输入的年份有误");
            }
            Console.ReadKey();
        }
    }
}

result

请输入年份:
2000
请输入月份
2
2000年2月有29天

resource

  • [文档] docs.microsoft.com/zh-cn/dotnet/csharp
  • [规范] github.com/dotnet/docs/tree/master/docs/standard/design-guidelines
  • [源码] referencesource.microsoft.com
  • [ IDE ] visualstudio.microsoft.com/zh-hans
  • [.NET Core] dotnet.github.io


感恩曾经帮助过 心少朴 的人。
C#优秀,值得学习。.NET Core具有跨平台的能力,值得关注。
Console,WinForm,WPF,ASP.NET,Azure WebJob,WCF,Unity3d,UWP可以适当地了解。
注:此文是自学笔记所生,质量中下等,故要三思而后行。新手到此,不可照搬,应先研究其理象数,待能变通之时,自然跳出深坑。

欢迎关注微信公众号:悟为生心

C#基础 try-catch 输入年月,判断天数_.net