源代码
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进行重复测试。求高人解释,谢谢。。
java问题判断闰年
答案:2 悬赏:0
解决时间 2021-12-21 04:50
- 提问者网友:生亦何欢
- 2021-12-20 14:06
最佳答案
- 二级知识专家网友:野心和家
- 2021-12-20 14:36
对代码修改了两处:
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();
}
}
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();
}
}
全部回答
- 1楼网友:承载所有颓废
- 2021-12-20 15:40
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;
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯