该程序应该检查输入的年份是否为闰年。但编译时我已经遇到了错误。 检查,如果它是一个闰年的计算公式如下:3条件中的一条if语句
If you can divide the year by 4 it's a leap year...
unless you can at the same time also divide it by 100, then it's not a leap year...
unless you can at the same time divide it by 400 then it's a leap year.
public class Schaltjahr {
public static void main (String[] args) {
double d;
String eingabe;
eingabe = JOptionPane.showInputDialog("Gib das Jahr ein "); //Type in the year
d = Double.parseDouble(eingabe);
if ((d % 4 == 0) & (d % 100 == 0) && (d % 400 = 0)) {
JOptionPane.showMessageDialog(null,"Es ist ein Schaltjahr"); //It is a leap year
} else {
if ((d % 4 == 0) & (d % 100 == 0))) {
JOptionPane.showMessageDialog(null, "Es ist kein Schaltjahr"); //It is not a leap year
}
} else {
if (d % 4 == 0) {
JOptionPane.showMessageDialog(null, "Es ist ein Schaltjahr"); // It is a leap year
}
}
}
}
在编译时我收到此错误:
Schaltjahr.java:16: error: illegal start of expression
if ((d % 4 == 0) & (d % 100 == 0))) {
^
Schaltjahr.java:19: error: 'else' without 'if'
} else {
^
2 errors
因为我希望你知道什么'else'意思..程序应该如何决定使用哪'else'块? – Tom
而不是写'else {if(...){...}}',写'else if(...){...}'。 – Gendarme
另外,当你指'&&'时,你在几个地方使用'&'。 – Gene