如何使Java闰年程序循环?
我必须为一个班级制作一个程序,告诉你一年是否是闰年。我已经完成了程序的基本功能,但我希望它能够询问用户是否希望再次执行整个操作(如果他们输入yes,它将重复该程序。如果他们输入no,程序将终止)。我更愿意使用while循环,但我不太清楚该怎么做。感谢您的帮助如何使Java闰年程序循环?,java,Java,我必须为一个班级制作一个程序,告诉你一年是否是闰年。我已经完成了程序的基本功能,但我希望它能够询问用户是否希望再次执行整个操作(如果他们输入yes,它将重复该程序。如果他们输入no,程序将终止)。我更愿意使用while循环,但我不太清楚该怎么做。感谢您的帮助 System.out.println("This program will let you enter a year and it will tell you" + " if it is a lear year"); int
System.out.println("This program will let you enter a year and it will tell you" +
" if it is a lear year");
int lyear;
Scanner scan = new Scanner(System.in);
System.out.println("Enter a valid gregorian year(greater than 1581): ");
lyear = scan.nextInt();
boolean LeapYear = ((lyear % 4 == 0) && (lyear % 100 != 0) || (lyear % 400 == 0));
if (lyear < 1582)
{
System.out.println("That is not a gregorian year");
}
else if (LeapYear)
{
System.out.println("That is a leap year");
}
else
{
System.out.println("That is not a leap year");
}
}
System.out.println(“这个程序会让你输入一年,它会告诉你”+
“如果是李尔王年”);
内耳;
扫描仪扫描=新扫描仪(System.in);
System.out.println(“输入有效的公历年(大于1581):”;
lyear=scan.nextInt();
布尔LeapYear=((lyear%4==0)和&(lyear%100!=0)| |(lyear%400==0));
if(lyear<1582)
{
System.out.println(“这不是公历年”);
}
否则,如果(年)
{
System.out.println(“那是闰年”);
}
其他的
{
System.out.println(“那不是闰年”);
}
}
制作一个函数checkLeapYear()
,并将当前代码复制到其中
现在编写while
循环:
boolean repeat = true;
while (repeat) {
checkLeapYear();
//prompt user to enter yes/no if they want to run again
String answer = ...
repeat = answer.equalsIgnoreCase("yes");
}
您可以尝试继续执行以下代码段-
while(scan.hasNext()){
line = scan.readLine();
if(line.equalsIgnoreCase("yes"){
continue;
}esle{
try{
int number = Integer.parseInt(line);
}catch(NumberFormatException nfe){}
}
}