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 に答える