Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使Java闰年程序循环?_Java - Fatal编程技术网

如何使Java闰年程序循环?

如何使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

我必须为一个班级制作一个程序,告诉你一年是否是闰年。我已经完成了程序的基本功能,但我希望它能够询问用户是否希望再次执行整个操作(如果他们输入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 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){}
   }
}