I'm new on this java programming. And I need to make this program loop the Question "Do you want to Try Again?! [Yes/No]" and loop it back to "Enter any year:" question. How will I do that?
我希望您为我提供更详细的答案或示例,以使该程序在今天内正常运行..
import java.util.Scanner;
public class my_LeapYear {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter any year: ");
int year = s.nextInt();
boolean loop = true;
while(loop == true)
{
if (((year %4==0)&& (year %100 !=0))||(year %400==0))
{
System.out.println("Year "+year+" is a Leap Year");
break;
}
else if ((year %100 == 0)&&(year %400 == 0))
{
} else {
System.out.println("Year "+year+" is not a Leap Year");
loop = false;
break;
}
最佳答案
尝试使用if
语句检查是否输入了yes或no,相应地更新loop
的值,如下所示
System.out.println("Do you want to Try Again")
String input = in.nextLine();
if (input.equals("YES")){
loop = true
}
else if (input.equals("NO")){
loop=false
}
关于java - 我将如何循环这个问题为 "Do you want to Try Again?! [Yes/No]"的java程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58831240/