年が閏年かどうかを判断する
年が4で除かれ、100で除かれない場合、または400で除かれる場合、この年は閏年です.ここでは型取りで演算を行います.
package net.ma;
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
long year = 1;
while (year != 0) {
System.out.println("Please input a year:");
year = scan.nextLong();
if(year%4 == 0 && year%100 != 0 || year%400 == 0) {
System.out.println(year + " is a leap year!");
} else {
System.out.println(year + " is not a leap year!");
}
}
}
}