确定使用Java的leap年? [英] Determine leap year using Java?

查看:179
本文介绍了确定使用Java的leap年?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的要求.确定年份是否为a年算法:

Here is my requirements. Determine If a Year Is a Leap Year Algorithm:

  • 如果年份可以平均除以4,则表示a年
    • 除非年份可以平均除以100,否则它不是a年
    • If the year can be evenly divided by 4, then it is a leap year
      • Except when the year can be evenly divided by 100, then it is not a leap year

      我需要知道我做得对吗?

      I need to know if i did right?

      private static boolean isLeapYear(int userInput){
          boolean leapYear= false;
      
          if (userInput % 4 == 0 ){
              leapYear = true;
      
              if (userInput % 4 == 0 && userInput % 100 ==0) {
                  leapYear = false;
      
                  if(userInput % 400 == 0){
                      leapYear = true;
                  }
              }
          }
          else {
              leapYear = false;
          }
      
          return leapYear;
      }
      

      推荐答案

      userInput % 4 == 0 && userInput % 100 ==0等效于userInput % 400 == 0

      userInput % 4 == 0,那么绝对是Le年,因此无需检查其他任何情况.

      and userInput % 4 == 0 then it is definitely Leap year so need not to check any other condition.

      这篇关于确定使用Java的leap年?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆