Java implementation method for determining whether a given year is a leap year

Source: Internet
Author: User
(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 ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.