ibcadmin 发表于 2015-7-29 09:31:12

c#判断闰年代码分享

//try 没增加异常数据处理
            Console.WriteLine("根据输入的信息计算当年某个月份的天数,以及当年是否是闰年或平年,\n并判断2月份特殊月份的天数。");
            Console.WriteLine("请输入需要计算的年份:");
            int year = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入需要获取的月份");
            int month = Convert.ToInt32(Console.ReadLine());

         
            switch (month)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                  Console.WriteLine("是31天");
                  break;
                case 2:
                  if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0)
                        //闰年的判断条件格式
                        // 如果 年能被400整除 或者 年能被4整除 并且年不能被100整除。

                  {
                        Console.WriteLine("是闰年,2月当月的天数有29天");
                  }
                  else
                  {
                        Console.WriteLine("是平年,2月当月的天数有28天");
                  }
                  break;
                default:
                  Console.WriteLine("为30天");
                  break;
            }

SHOKUZ 发表于 2015-7-31 16:02:43

。。。。。。。。
页: [1]
查看完整版本: c#判断闰年代码分享