Java 如果表达式有错误,则跳过
每当我输入一个闰年值时,程序就会平稳运行并执行它应该执行的操作:Java 如果表达式有错误,则跳过,java,leap-year,Java,Leap Year,每当我输入一个闰年值时,程序就会平稳运行并执行它应该执行的操作: import java.util.*; public class LeapYear { public static void main (String args[]) { Scanner scan = new Scanner(System.in); int userInput = scan.nextInt(); boolean leapYearisTrue = fals
import java.util.*;
public class LeapYear {
public static void main (String args[]) {
Scanner scan = new Scanner(System.in);
int userInput = scan.nextInt();
boolean leapYearisTrue = false;
while ( userInput != 0 ) {
if (userInput % 4 == 0) {
if ( (userInput % 100 == 0) && (userInput % 400 != 0) ) {
leapYearisTrue = false;
System.out.println (leapYearisTrue);
}
else {
leapYearisTrue = true;
System.out.println (leapYearisTrue);
}
userInput = scan.nextInt();
}
}
}
}
但每当我输入一个非闰年时,它不会打印false,也不会再打印一个数字是闰年:
2000
true
1960
true
400
true
您需要将
else
条件添加到if(userInput%4==0)
条件。您需要将else
条件添加到if(userInput%4==0)
条件。尝试以下操作:
403
400
2000 ( this is a leap year , yet it doesn't print true)
2004
试试这个:
403
400
2000 ( this is a leap year , yet it doesn't print true)
2004
你错过了一个如果…否则你错过了一个如果…否则你就像一个符咒,你就是一个神。我会在5分钟内给你一个答案。你工作得很有魅力,你就是上帝。我会在5分钟内给你答复。