del
每当我输入一个闰年的值时,程序就会顺利运行并完成预期的操作:
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();
}
}
}
}
但每当我输入一个非闰年时,它就不会打印错误,并且不再打印数字是闰年:
2000
true
1960
true
400
true
答案 0 :(得分:2)
您需要将else
条件添加到if (userInput % 4 == 0)
条件。
答案 1 :(得分:1)
试试这个:
import java.util.*;
public class LeapYear {
public static void main(String args[]) {
System.out.println("Enter the year: \n");
Scanner scan = new Scanner(System.in);
int userInput = scan.nextInt();
boolean leapYearisTrue = false;
while (userInput != 0) {
if (userInput % 4 == 0) {
if (userInput % 100 == 0) {
if (userInput % 400 != 0) {
leapYearisTrue = true;
System.out.println(leapYearisTrue);
}
} else {
leapYearisTrue = true;
System.out.println(leapYearisTrue);
}
userInput = scan.nextInt();
}
}
}
}