Java 程序:检查闰年
原文:https://www.studytonight.com/java-programs/java-program-to-check-leap-year
闰年是有 366 天的一年。如果满足以下条件,则称一年为闰年:
- 年份是 400 的倍数。
- 年份是 4 的倍数,但不是 100。
在这里,给我们一年,我们的任务是检查给定的一年是否是闰年。例如,
输入: 2019
产量:不是闰年
程序 1:检查闰年
在这个方法中,我们会在主方法本身中直接检查一年是否是闰年。
算法
- 开始
- 声明一个变量,比如一年。
- 初始化它。
- 检查条件。
- 如果条件满足,那就是闰年,否则就不是。
- 显示结果。
- 停下来。
下面是相同的代码。
在下面的程序中,我们在主方法本身中检查给定的年份是否是闰年。
//Java Program to check whether the given year is a leap year or not
import java.util.Scanner;
public class CheckYear
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
int year; //Year Declaration
System.out.println("Enter the year");
year=sc.nextInt(); //Year Initialization
//Check for leap year
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");
}
}
进入 1998 年 1998 年不是闰年
程序 2:检查闰年
在这个方法中,我们将使用条件运算符检查给定的年份是否是闰年。
算法:
- 开始
- 声明一个变量,比如一年。
- 初始化它。
- 使用三元运算符检查给定的年份是否是闰年。
- 在三进制运算符的条件部分调用一个方法进行检查。
- 如果年份是 400 的倍数,则返回 true。
- 否则,如果年份是 100 的倍数,则返回 false。
- 否则,如果年是 4 的倍数,那么它是闰年,返回真。否则返回 false。
- 停下来。
下面是相同的代码。
下面的例子演示了如何使用三元运算符来检查闰年。
//Java Program to check whether the given year is a leap year or not
import java.util.Scanner;
public class CheckYear
{
static boolean checkLeapYear(int year)
{
// If a year is multiple of 400, then it is a leap year
if (year % 400 == 0)
return true;
// Else If a year is multiple of 100, then it is not a leap year
if (year % 100 == 0)
return false;
// Else If a year is multiple of 4, then it is a leap year
if (year % 4 == 0)
return true;
return false;
}
// Driver method
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
int year; //Year Declaration
System.out.println("Enter the year");
year=sc.nextInt(); //Year Initialization
//Ternary Operator to check
System.out.println( checkLeapYear(2000)? "Leap Year" :
"Not a Leap Year" );
}
}
进入 2012 年 闰年
程序 3:检查闰年
在这个方法中,我们将使用函数检查给定的年份是否是闰年。
算法:
- 开始
- 声明一个变量,比如说年份。
- 初始化它。
- 调用函数进行检查。
- 如果年份是 400 的倍数,则返回 true。
- 否则,如果年份是 100 的倍数,则返回 false。
- 否则,如果年份是 4 的倍数,那么它就是闰年,并返回 true。
- 使用 if-else 条件显示结果。
- 停下来。
下面是相同的代码。
下面的例子演示了如何使用函数来检查闰年。
//Java Program to check whether the given year is a leap year or not using Functions
import java.util.Scanner;
public class CheckYear
{
static boolean checkLeapYear(int year)
{
// If a year is multiple of 400,
// then it is a leap year
if (year % 400 == 0)
return true;
// Else If a year is multiple of 100,
// then it is not a leap year
if (year % 100 == 0)
return false;
// Else If a year is multiple of 4,
// then it is a leap year
if (year % 4 == 0)
return true;
return false;
}
// Driver method
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
int year; //Year Declaration
System.out.println("Enter the year");
year=sc.nextInt(); //Year Initialization
boolean check=checkLeapYear(year);
if(check)
{
System.out.println(year+" is a leap year");
}
else
{
System.out.println(year+" is not a leap year");
}
}
}
进入 2018 年 2018 年不是闰年