We know that (1) If it is a year of a hundred, the one that can be divisible by 400 is a leap year; (2) If it is not a year of a hundred, the one that can be divisible by 4 is a leap year. Every 400 years, there are 97 leap years. In view of this, the program can be designed as follows:
The first step is to judge whether the year is divided by 400. If possible, it will be a leap year. For example, 1600, 2000, and 2400 are leap years.
The second step is to judge whether the year can be divided by 100 based on the fact that the first step is not valid. If so, it is not a leap year. For example, 1900, 2100, and 2200 are not leap years.
The third step is to judge whether the year can be divided by 4 based on the second step, and if so, it is a leap year. For example, 1996, 2004, and 2008 were leap years.
The fourth step, on the basis that the third step is not a leap year. For example, 1997, 2001, and 2002 are not leap years.
import java.util.Scanner;//Insert the scanner public class runnian{ public static void main(String[] args)//Sting[] args Don't forget to write it in { Scanner s=new Scanner(System.in);//Declare the scanner variable System.out.println("Please enter year");//The system prompts to enter year int nianfen=s.nextInt();//Get the year value entered in the next line if(nianfen%400==0){System.out.println(nianfen+"Year is a leap year");}//Judge whether it can be divisible by 400else if(nianfen%100==0){System.out.println(nianfen+"year is not a leap year");}//Judge whether it can be divisible by 100else if(nianfen%4==0){System.out.println(nianfen+"year is a leap year");}//Judge whether it can be divisible by 4else{System.out.println(nianfen+"year is not a leap year");}}}After preliminary testing, this program can correctly determine whether it is a leap year. If there are any errors in this program, please correct it. Everyone must have other implementation methods, welcome to reply and provide.
================================
After learning other people's related video teaching, I wrote the second implementation method, which can only use one if-else statement. The code is as follows:
import java.util.Scanner;public class runnian{ public static void main(String[] args) { Scanner s=new Scanner(System.in); System.out.println("Please enter year"); int nianfen=s.nextInt(); if(nianfen%4==0&&nianfen%100!=0||nianfen%400==0){System.out.println(nianfen+"Year is a leap year");} //The year can be divisible by 4 but cannot be divisible by 100, or the year can be divisible by 400 else{System.out.println(nianfen+"year is not a leap year");} }}The above is the entire content of the simple examples that the editor brings to you about whether it is a leap year using Java programs. I hope everyone supports Wulin.com more~