天天看点

判断闰年代码如下:

闰年是一个比较简单而又经典的数学问题。

判断闰年的算法也是很简单的,一个基本规则就是:四年一闰,百年不闰,四百年再闰

具体点说就是闰年是能够被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;
	}
}
           
判断闰年代码如下:

继续阅读