用java程序判断输入年份是否为闰年,如果输入错误,能重新输入,知道输入正确为止

用java程序判断输入年份是否为闰年,如果输入错误,能重新输入,知道输入正确为止,第1张

public class Bissextile {
public static void main(String[] args) {
Bissextile instance = new Bissextile();
while (!instanceisBissextile()) {
Systemoutprintln("不是闰年\n");
}
Systemoutprintln("是闰年");
}
private boolean isBissextile() {
Systemoutprintln("请输入年份:");
Scanner scanner = new Scanner(Systemin);
int year = scannernextInt();
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
}

/

/
/
@author qingsongwang

/
import javautil;
public class B {
public static boolean isRun(int year)
{
return (year % 4 != 0 )|| (year % 100 == 0 )&& (year % 400 != 0);

}
public static void main(String[] args) {
Scanner input=new Scanner(Systemin);
int year;
Systemoutprintf("你输入的年份是:%s", year=inputnextInt());
if(isRun(year)){
Systemoutprint("(是闰年)");
}
else{
Systemoutprint("(不是闰年)");
}
}
}
直接运行这个程序即可!

今年的年数和月数知道吧?
看月数是否小于或等于2,如果是就判断今年是否闰年,如果否判断明年是否闰年
如果前面的判断闰年为真,那么明年的今天 就是当前的时间+ 3662436001000
如果闰年判断为假就是365243600100了
判断当年:
年数=n
是否闰年 = ((n/4==0 && n/100!=0) || n/400 == 0)
public static void main(String[] args) {
Date now = new Date();
int nowYear = nowgetYear();
int month = nowgetMonth();
long addMillis = 0;
boolean isLeap = false;
if(month <= 2){
isLeap = ((nowYear%4==0 && nowYear%100!=0) || nowYear%400 == 0);
}else{
nowYear += 1;
isLeap = ((nowYear%4==0 && nowYear%100!=0) || nowYear%400 == 0);
}
if(isLeap){
addMillis = ((long)366)2436001000;
}else{
addMillis = ((long)365)2436001000;
}

Date nextYear = new Date(SystemcurrentTimeMillis() + addMillis);
Systemoutprintln(nextYear);
}


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

原文地址: http://outofmemory.cn/yw/13103404.html

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

发表评论

登录后才能评论

评论列表(0条)

保存