Java 從1970到20xx某一年中閏年的數量

public class LeapYear {
  int 閏年數量 = 0/**
  * 閏年的條件是:四年一閏,百年不閏,四百年再閏
  * 1、能被4整除,但不能被100整除;
  * 2、能被100整除,又能被400整除。
  * 本文檔以1970~2011年爲例,可以自己更改
  * @param year
  * @return
  */
 public static boolean isLeapYear(int year){
  return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ;
 }

 public static void main(String[] args) {
  for(int i=1970; i<=2011; i++){
   if(isLeapYear(i)){
    System.out.println(""+i+"是閏年");
    閏年數量++;
   }
  }
    System.out.println("從1970~2011閏年的數量爲:"+閏年數量);
 }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章