查找给定年份是否为闰年的Java程序
闰年有 366 天,每四年一次。每个闰年都对应于以下事实:
- 世纪年是以00结尾的年份。世纪年只有能被400整除才是闰年。
- 如果闰年能被 4 整除,则可以确定闰年(世纪年除外)。
- 一个世纪的年份应该可以被 4 和 100 整除。
- 非世纪年份只能被 4 整除。
让我们找出一年是否是闰年。
不使用扫描器类
顾名思义,这里用户一定不会选择他选择的年份。以下程序说明了确定年份是否为闰年的方法:
Java
// Java program to find a leap year
// Importing Classes/Files
import java.io.*;
// Class for leap-year dealing
public class GeeksforGeeks {
// Method to check leap year
public static void isLeapYear(int year)
{
// flag to take a non-leap year by default
boolean is_leap_year = false;
// If year is divisible by 4
if (year % 4 == 0) {
// To identify whether it
// is a century year or
// not
if (year % 100 == 0) {
// Checking if year is divisible by 400
// therefore century leap year
if (year % 400 == 0) {
is_leap_year = true;
}
else {
is_leap_year = false;
}
}
// Out of if loop that is Non century year
// but divisible by 4, therefore leap year
is_leap_year = true;
}
// We land here when corresponding if fails
// If year is not divisible by 4
else
// Flag dealing- Non leap-year
is_leap_year = false;
if (!is_leap_year) {
System.out.println(year + " : Non Leap-year");
}
else {
System.out.println(year + " : Leap-year");
}
}
// Main Driver Code
public static void main(String[] args)
{
// Calling our function by
// passing century year
isLeapYear(2000);
// Calling our function by
// passing Non-century year
isLeapYear(2002);
}
}
Java
// Java program to check Leap-year
// by taking input from user
// Importing Classes/Files
import java.io.*;
// Importing Scanner Class
import java.util.Scanner;
// Class to check leap-year or not
public class GFG {
// Driver Main code
public static void main(String[] args)
{
// Considering any random year
int year;
// Taking input from user using Scanner Class
// scn is an object made of Scanner Class
Scanner scn = new Scanner(System.in);
year = scn.nextInt();
// 1st condition check- It is century leap year
// 2nd condition check- It is leap year and not
// century year
if ((year % 400 == 0)
|| ((year % 4 == 0) && (year % 100 != 0))) {
// Both conditions true- Print leap year
System.out.println(year + " : Leap Year");
}
else {
// Any condition fails- Print Non-leap year
System.out.println(year + " : Non - Leap Year");
}
}
}
输出:
2000 : Leap-year
2002 : Non Leap-year
2000 是一个世纪年,可以被 100 和 4 整除。2002 年不能被 4 整除,因此不是闰年。
使用扫描器类
在这里,用户可以灵活地输入自己选择的年份,因为在此处导入扫描仪类,其余 if-else 块也组合在一个语句中,以检查输入年份是否为闰年。以下程序说明了确定年份是否为闰年的方法:
Java
// Java program to check Leap-year
// by taking input from user
// Importing Classes/Files
import java.io.*;
// Importing Scanner Class
import java.util.Scanner;
// Class to check leap-year or not
public class GFG {
// Driver Main code
public static void main(String[] args)
{
// Considering any random year
int year;
// Taking input from user using Scanner Class
// scn is an object made of Scanner Class
Scanner scn = new Scanner(System.in);
year = scn.nextInt();
// 1st condition check- It is century leap year
// 2nd condition check- It is leap year and not
// century year
if ((year % 400 == 0)
|| ((year % 4 == 0) && (year % 100 != 0))) {
// Both conditions true- Print leap year
System.out.println(year + " : Leap Year");
}
else {
// Any condition fails- Print Non-leap year
System.out.println(year + " : Non - Leap Year");
}
}
}
输入:
2012
输出:
2012 : Leap Year