
Copy code
import java.util.Scanner
public class LeapYear {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in)
System.out.print("请输入待判断的年份:")
int year = sc.nextInt()
boolean isLeapYear = false
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
isLeapYear = true
} else {
isLeapYear = false
}
} else {
isLeapYear = true
}
} else {
isLeapYear = false
}
if (isLeapYear) {
System.out.println(year + "是闰年。")
} else {
System.out.println(year + "不是闰年。")
}
}
}
这个程序中,首先提示用户输入一个年份,然后使用一个布尔变量isLeapYear来存储程序是否判断为闰年,如果是闰年,则为true,否则为false。然后使用嵌套的if语句来判断年份是否为闰年。如果年份可以被4整除,则可能是闰年,然后判断它是否也可以被100整除。如果年份可以被100整除,则只有当它同时能被400整除时才是闰年。如果年份不是可以被4整除的,则不是闰年。
最后使用if语句和输出语句来告诉用户输入的年份是否是闰年。
其实就是看2000~2050能被4整除的数,代码为:
public class Main {
public static void main(String[] args) {
System.out.println("2000~2050年之间的闰年有:")
for (int i = 2000i <= 2050i++) {
if (i % 4 == 0) {
System.out.print(i + " ")
}
}
}
}
运行结果:
代码如下:public class RUN {
public static void main(String[] args) {
//布尔型判断
int year = 2000
boolean b1 = year%4==0
boolean b2 = year%100!=0
boolean b3 = year%400==0
if(b1&&b2||b3){
System.out.println("闰年")
}else{
System.out.println("不是闰年")
}
//用if语句判断
int year2=2018
if(year2 % 4 == 0 &&year2 % 100 != 0 || year2 % 400 == 0){
System.out.println("是闰年")
}else{
System.out.println("不是闰年")
}
}
}
代码截图: 扩展资料:闰年是公历中的名词。闰年分为普通闰年和世纪闰年。
普通闰年:能被4整除但不能被100整除的年份为普通闰年。(如2004年就是闰年,1999年不是闰年);
世纪闰年:能被400整除的为世纪闰年。(如2000年是闰年,1900年不是闰年);
闰年(Leap Year)是为了弥补因人为历法规定造成的年度天数与地球实际公转周期的时间差而设立的。补上时间差的年份为闰年。闰年共有366天(1-12月分别为31天,29天,31天,30天,31天,30天,31天,31天,30天,31天,30天,31天)。
凡阳历中有闰日(二月为二十九日)的年;闰余(岁余置闰。阴历每年与回归年相比所差的时日);
注意闰年(公历中名词)和闰月(农历中名词)并没有直接的关联,公历中只分闰年和平年,平年有365天,而闰年有366天(2月中多一天);
平年中也可能有闰月(如2017年是平年,农历有闰月,闰6月)。
参考资料:百度百科-闰年
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)