(1) If a year can be divisible by 4 but cannot be divisible by 100, it is a leap year.
(2) If a year can be divisible by 400, it is also a leap year.
Procedure 1:
Package exercise;
Import java. util. Collections;
Import java. util. regex. Matcher;
Import java. util. regex. Pattern;
Public class IsLeapYear {
Public static boolean isNum (String str ){
Boolean isNum = true;
For (int I = 0; I <str. length (); I ++ ){
// Mark if the year is a number;
IsNum = false;
If (str. codePointAt (I)> 47 & str. codePointAt (I) <58 ){
IsNum = true;
}
If (isNum = false ){
Break;
}
} //
Return isNum;
}
Public void isHeapYear (){
Int year = 0;
System. out. println ("Please input the year you want to judge :");
Running in = new processing (System. in );
String temp = in. next ();
If (! IsNum (temp )){
System. out. println ("the year you input is not valid ");
} Else {
Year = Integer. parseInt (temp );
If (year <1980 | year> 2050 ){
System. out. println ("the year you input is not in the scale ");
System. exit (1 );
} Else 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 ");
}
}
}
Public static void main (String [] args ){
New IsLeapYear (). isHeapYear ();
}
}
Procedure 2:
Package exercise;
Import java. util. Collections;
Import java. util. regex. Matcher;
Import java. util. regex. Pattern;
Public class IsLeapYearReg {
Public static boolean isNum (String str ){
Pattern p = Pattern. compile ("[0-9] *");
Matcher isNum = p. matcher (str );
If (isNum. matches ()){
Return true;
} Else {
Return false;
}
}
Public void isHeapYear (){
Int year = 0;
System. out. println ("Please input the year you want to judge :");
Running in = new processing (System. in );
String temp = in. next ();
If (! IsNum (temp )){
System. out. println ("the year you input is not valid ");
} Else {
Year = Integer. parseInt (temp );
If (year <1980 | year> 2050 ){
System. out. println ("the year you input is not in the scale ");
System. exit (1 );
} Else 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 ");
}
}
}
Public static void main (String [] args ){
New IsLeapYearReg (). isHeapYear ();
}
}