编写程序,显示从101到2100期间所有闰年,每行显示10个。数字之间用一个空格字符隔开,同时显示这期间闰年的数目。
package pack2;
import java.util.Scanner;
public class ShowLeapYears {
public static void main(String[] args) {
try(Scanner input = new Scanner(System.in);) {
System.out.print("Enter start year and end year: ");
showLeapYears(input.nextInt(), input.nextInt());
}
}
//显示闰年
public static void showLeapYears(int start, int end) {
int sum = 0;
for (int i = start, j = 1; i <= end; i++)
if(isLeapYear(i)) {
System.out.printf("%5d", i);
if(j++ % 10 == 0) System.out.println();
sum++;
}
System.out.println("\nThe number of leap year from "+start+" to "+end+
" has "+sum);
}
//判定是否为闰年
public static boolean isLeapYear(int year) {
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
return true;
return false;
}
}