天天看點

判斷閏年代碼如下:

閏年是一個比較簡單而又經典的數學問題。

判斷閏年的算法也是很簡單的,一個基本規則就是:四年一閏,百年不閏,四百年再閏

具體點說就是閏年是能夠被4整除,但不能被100整除,有一點例外是可以被400整除的年份。

代碼如下:

代碼實作的是輸出21世紀的所有閏年

package 判斷閏年;

public class Example {
	public static void main(String[] args) {
		int count = 0;
		System.out.println("21世紀所有的閏年如下:\n");
		for (int i = 2000; i < 2100; i++) {
			if (judge(i) == 1) {
				System.out.print(i + " ");
				count++;
			}
			if (count % 10 == 0)
				System.out.println();
		}
	}

	static int judge(int year) {
		//四年一閏,百年不閏,四百年再閏
		if (year % 4 == 0 && year % 100 != 0)
			return 1;
		else if (year % 400 == 0)
			return 1;
		else
			return 0;
	}
}
           
判斷閏年代碼如下:

繼續閱讀