编译我的java程序后,我遇到了一堆错误,我不知道如何解决它们。任何人都可以帮我一把吗? = /
class Date{
public static boolean isLeapYear(int year){
boolean answer = false;
if(year % 4 == 0 &&(year % 400 == 0 || year % 100 != 0))
answer = true;
else
answer = false;
return answer;
}
}
int returnDaysInMonth (int year, int month){
if (month==1||month==3||month==5||month==7||month==8||month==10||month==12){
return 31;
} else if (month==4||month==6||month==9||month==11){
return 30;
} else if(isLeapYear(year)){
return 29;
} else {
return 28;
}
}
以下是编译错误:
/Users/vlop/NetBeansProjects2/JavaLibrary1/src/Date.java:22: class, interface, or enum expected
public int returnDaysInMonth (int year, int month){
/Users/vlop/NetBeansProjects2/JavaLibrary1/src/Date.java:28: class,
interface, or enum expected
} else if (month==4||month==6||month==9||month==11){
/Users/vlop/NetBeansProjects2/JavaLibrary1/src/Date.java:32: class,
interface, or enum expected
} else if(isLeapYear(year)){
/Users/vlop/NetBeansProjects2/JavaLibrary1/src/Date.java:35: class,
interface, or enum expected
} else {
/Users/vlop/NetBeansProjects2/JavaLibrary1/src/Date.java:38: class,
interface, or enum expected
}
5 errors
答案 0 :(得分:1)
你的类声明关闭后,你正试图做一堆Java。
正确的缩进有助于识别此类问题:
class Date {
public static boolean isLeapYear(int year) {
boolean answer = false;
if(year % 4 == 0 &&(year % 400 == 0 || year % 100 != 0))
answer = true;
else
answer = false;
return answer;
}
} // End of class.
// WAT
int returnDaysInMonth (int year, int month) {
// etc.
另外,单独的问题,但您可以直接返回布尔表达式的结果:
public static boolean isLeapYear(int year) {
return ((year % 4 == 0) && ((year % 400 == 0) || (year % 100 != 0)));
}
即使你对此不满意,你仍然可以避免一些噪音:
public static boolean isLeapYear(int year) {
if (year % 4 == 0 &&(year % 400 == 0 || year % 100 != 0)) {
return true;
}
return false;
}
答案 1 :(得分:0)
你需要围绕方法的括号
答案 2 :(得分:0)
你的括号都搞砸了导致这个类无法解析,你是否正在使用编译器或eclipse之类的程序来编程,因为它应该告诉你存在问题
答案 3 :(得分:0)
这些是查找闰年的简化方法。
public static boolean isLeapYear(int year) {
assert year >= 1583; // not valid before this date.
return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
答案 4 :(得分:0)
它看起来应该是这样的
class Date {
public static boolean isLeapYear(int year) {
boolean answer = false;
if (year % 4 == 0 && (year % 400 == 0 || year % 100 != 0))
answer = true;
else
answer = false;
return answer;
}
int returnDaysInMonth(int year, int month) {
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
|| month == 10 || month == 12) {
return 31;
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
return 30;
} else if (isLeapYear(year)) {
return 29;
} else {
return 28;
}
}
}