中易网

java问题判断闰年

答案:2  悬赏:0  
解决时间 2021-12-21 04:50
源代码

import java.util.*;

public class MoreLeapYear {

public static void main(String[] args)
{
int year;
String another = "y";
Scanner scan= new Scanner(System.in);
System.out.println("please enter a year!");
year = scan.nextInt();

while (another.equalsIgnoreCase("y"))
{
if(year<1582)
System.out.println("sorry! there is no Solar calendar at that time!");
else
{
if(year%4==0&&year%100!=0||year%100==0&&year%400==0)
System.out.println("this year is a leap year!");
else
System.out.println("this year is not a leap year!");
}

System.out.println();

System.out.print("Test another year?(y/n) ");
another = scan.nextLine();
}

}
}
最后面无法输入y进行重复测试。求高人解释,谢谢。。
最佳答案
对代码修改了两处:

1)System.out.println("please enter a year!");
year = scan.nextInt();
将这两行代码放到了while循环里,因为你的year要一直接受值,而不是一次,放到外面就接受不到了,至于上面那一行,是为了程序和人的交互好一点.

2)another = scan.nextLine();
将最后一行代码改成了another = scan.next();nextLine就不是这么用了,这里用next()就可以解决问题.

修改后的代码如下:

public static void main(String[] args)
{
int year;
String another = "y";
Scanner scan= new Scanner(System.in);

while (another.equalsIgnoreCase("y"))
{
System.out.println("please enter a year!");
year = scan.nextInt();
if(year<1582){
System.out.println("sorry! there is no Solar calendar at that time!");
}
else
{
if(year%4==0&&year%100!=0||year%100==0&&year%400==0){
System.out.println("this year is a leap year!");
}
else{
System.out.println("this year is not a leap year!");
}
}

System.out.println("Test another year?(y/n) ");
another = scan.next();
}

}
全部回答
import java.util.*; public class bijiao { public static void main(string[] args) { system.out.println("请输入年份:"); scanner sc=new scanner(system.in); int year=sc.nextint(); //输入年份 if(runnian(year)) { system.out.println(year+"年是闰年"); } else { system.out.println(year+"年不是闰年"); int p=year; while(!runnian(p-1)) { p--; } system.out.println(year+"年之前的闰年是"+(p-1)); p=year; while(!runnian(p+1)) { p++; } system.out.println(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; } }
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
苹果笔记本PRO能装360安全卫士吗
一个长方体、正方体、圆柱、圆锥、说说他们图
关于气质的词语
大腿根部内侧被硬东西撞到,当时青了一大块,
不等式2x-1小于0的解是什么
圆通汽车怎么去啊,有事要去办理
h1z1昨天玩的好好的,今天让我验证邮箱,打不
一个两位数,个位上的数字是最大的一位数,这样
我国这一园林艺术的瑰宝……缩句
口袋妖怪漆黑魅影里为什么双打中我的图图犬素
您好,您的WIN7系统最后把S7-200的编程软件装
电信手机号开通翼支付有哪些优势?
武汉华东科技大学自闭证
一个女生不看电影,不喜欢逛街,怕走路,喜欢
大众途观复位环怎么拆卸
推荐资讯
有没有人知道这个表情的表情包叫什么啊
请问你们说话的时候会紧张吗?说话前脑子会担
男朋友生日发什么短信祝福他
雅趣人生王纯阅读答案
是谁像曹操告诉孙策获取许都之心,后被孙策所
求lol大神请问学生手猴子和乌鸦那个好准备入
饑枵的意思是什么?饑枵的释义是什么啊?
摊小米煎饼怎么老粘锅
请问我之前买了一个原始股,公司一把手好像合
みんな元気ですか俺は今元気だよ!什么意思
蒸淀社区居委会办公地址在什么地方,我要处理
城埤的意思是什么啊?请解释下!
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?