判断闰年的条件

判断闰年的条件,第1张

判断闰年的条件如下:

公历年份是4的倍数,且不是100的倍数,为普通闰年。公历年份是整百数,且必须是400的倍数才是世纪闰年。归结起来就是通常说的:四年一闰;百年不闰,四百年再闰。

闰年是为了弥补因人为历法规定造成的年度天数与地球实际公转周期的时间差而设立的。地球绕太阳运行周期为365天5小时48分46秒,即一回归年。公历的平年只有365日,比回归年短约02422日,所余下的时间约为每四年累计一天,故第四年于2月末加1天,使当年的历年长度为366日,这一年就为闰年。

闰年(Leap Year)是为了弥补因人为历法规定造成的年度天数与地球实际公转周期的时间差而设立的。补上时间差的年份为闰年。闰年共有366天(1月~12月分别为31天、29天、31天、30天、31天、30天、31天、31天、30天、31天、30天、31天)。

凡阳历中有闰日(2月29日)的年份,闰余(岁余置闰。阴历每年与回归年相比所差的时日)。注意闰年(公历中的名词)和闰月(农历中的名词)并没有直接的关联,公历只分闰年和平年,平年有365天,闰年有366天(2月中多一天);平年中也可能有闰月(如2017年是平年,农历有闰月,闰六月)。

①、普通年能被4整除且不能被100整除的为闰年。(如2004年就是闰年,1900年不是闰年)
②、世纪年能被400整除的是闰年。(如2000年是闰年,1900年不是闰年)
if((year%4==0&&year%1!=0)||(year%400==0))
{
Systemoutprintln(year+"年是闰年");
}

import javautil;
public class bijiao
{
public static void main(String[] args)
{
Systemoutprintln("请输入年份:");
Scanner sc=new Scanner(Systemin);
int year=scnextInt(); //输入年份

if(runNian(year))
{
Systemoutprintln(year+"年是闰年");
}
else
{
Systemoutprintln(year+"年不是闰年");
int p=year;
while(!runNian(p-1))
{
p--;
}
Systemoutprintln(year+"年之前的闰年是"+(p-1));
p=year;
while(!runNian(p+1))
{
p++;
}
Systemoutprintln(year+"年之后的闰年是"+(p+1));
}
}
static boolean runNian(int year)//判断是否为闰年的方法
{
boolean t=false;
if(year%4==0)
{
if(year%100!=0)
{
t=true;
}
else if(year%400==0)
{
t=true;
}
}
return t;
}
}

以下为代码:

import javaapplet;

import javaawt;

import javaawtevent;

public class LeapyearTest extends Applet implements ActionListener{

Label lblResult;

Button btn;

TextField txt;

int year;

boolean leap;

public void init() {

lblResult=new Label("请输入要判断的年份");

txt=new TextField(5);

btn=new Button("判断");

add(lblResult);

add(txt);

add(btn);

btnaddActionListener(this);

}

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

year=IntegerparseInt(txtgetText());

if(year%4==0&;&;(year%100)!=0)

{leap=true;

}

else if(year%400==0){

leap=false;

}

if(leap==true)

lblResultsetText(year+"年是闰年");

else

lblResultsetText(year+"年是平年");

txtsetText("");

}

}

扩展资料:

在windows下编译java文件、执行:

1、先创建一个txt,更改为testjava。

2、编写代码,为输出为holloword。

3、找到cmd,并进行打开cmd。

4、编译java文件,输入命令为javac testjava。

5、如果没有报错,查看当前目录下是否有class文件产生。

6、执行class文件,在命令输入java test,输出为holloword。

闰年:能被4整除,但不能被100整除,或能被100整除,又能被400整除。
Scanner scan = new Scanner(Systemin);
int input = scannextInt();
if ((input % 4 == 0 && input % 100 != 0)
|| ( input % 400 == 0))
Systemoutprintln(input + "年闰年");
else
Systemoutprintln(input + "年平年");

publicvoidisLeapYear(intyears){Calendarcal=CalendargetInstance();calset(years,CalendarDECEMBER,31);if(calget(CalendarDAY_OF_YEAR)==366){Systemoutprintln(years+"年是闰年");}else{Systemoutprintln(years+"年平年");}}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13103466.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-30
下一篇 2023-05-30

发表评论

登录后才能评论

评论列表(0条)

保存