0

编译我的 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
4

5 回答 5

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;
}
于 2013-05-02T01:31:04.750 回答
0

你需要方法周围的括号

于 2013-05-02T01:30:06.753 回答
0

你的括号都搞砸了,导致类无法解析,你是在使用编译器还是像 eclipse 这样的 ide 来编程这个,因为它应该告诉你有问题

于 2013-05-02T01:30:49.497 回答
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);
}
于 2013-05-02T01:32:49.223 回答
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;

    }
}

}
于 2013-05-02T01:34:07.713 回答