0

文字列を返さなければならないと言っていますが、何か問題はありませんか? numericDayOfWeek は正常に動作するはずだと思いますか?

 public String getDayOfWeek(){
         if(numericDayOfWeek==0){
             return "Saturday";
         }
         if(numericDayOfWeek==1){
             return "Sunday";
         }
         if(numericDayOfWeek==2){
             return "Monday";
         }
        if(numericDayOfWeek==3){
             return "Tuesday";
         }
        if(numericDayOfWeek==4){
            return "Wednesday";
         }
        if(numericDayOfWeek==5){
            return "Thursday";
         }
         if(numericDayOfWeek==6){
             return "Friday";
         }
     }

ここに完全なコードがあります

public class DayOfWeek {
    int myMonth, myDayOfMonth, myYear, myAdjustment, numericDayOfWeek;

    public DayOfWeek(int month, int dayOfMonth, int  year){
        myMonth = month;
        myDayOfMonth = dayOfMonth;
        myYear = year;
    }
    public int getNumericDayOfWeek(){
        if(myMonth==1){
            myAdjustment = 1;
            if(myYear%4==0){
                myAdjustment-=1;
            }
        }
        if(myMonth==2){
            myAdjustment = 4;
            if(myYear%4==0){
                myAdjustment-=1;
            }
        }
        if(myMonth==3){
            myAdjustment = 4;
        }
        if(myMonth==4){
            myAdjustment = 0;
        }
        if(myMonth==5){
            myAdjustment = 2;
        }
        if(myMonth==6){
            myAdjustment = 5;
        }
        if(myMonth==7){
            myAdjustment = 0;
        }
        if(myMonth==8){
            myAdjustment = 3;
        }
        if(myMonth==9){
            myAdjustment = 6;
        }
        if(myMonth==10){
            myAdjustment = 1;
        }
        if(myMonth==11){
            myAdjustment = 4;
        }
        if(myMonth==12){
            myAdjustment = 6;
        }
        int fourDivides = myYear / 4;
        numericDayOfWeek = myAdjustment + myDayOfMonth + (myYear-1900) + fourDivides; 
        return numericDayOfWeek;

    }
     public String getDayOfWeek(){
         if(numericDayOfWeek==0){
             return "Saturday";
         }
         if(numericDayOfWeek==1){
             return "Sunday";
         }
         if(numericDayOfWeek==2){
             return "Monday";
         }
        if(numericDayOfWeek==3){
             return "Tuesday";
         }
        if(numericDayOfWeek==4){
            return "Wednesday";
         }
        if(numericDayOfWeek==5){
            return "Thursday";
         }
         if(numericDayOfWeek==6){
             return "Friday";
         }
     }
     public int getMonth(){

     }
     public String getMonthString(){

     }
     public int getDayOfMonth(){

     }
     public int getYear(){

     }

}
4

4 に答える 4

3

どの条件も満たさない場合、つまり. それらはすべて に評価されfalse、メソッドは何も返しません。最後にデフォルトの戻り値を追加します

public String getDayOfWeek(){
     if(numericDayOfWeek==0){
         return "Saturday";
     }
     if(numericDayOfWeek==1){
         return "Sunday";
     }
     if(numericDayOfWeek==2){
         return "Monday";
     }
     if(numericDayOfWeek==3){
         return "Tuesday";
     }
     if(numericDayOfWeek==4){
        return "Wednesday";
     }
     if(numericDayOfWeek==5){
        return "Thursday";
     }
     if(numericDayOfWeek==6){
         return "Friday";
     }
     return "Error";
 }

コンパイラはすべてのパスを考慮します。ifステートメントが実行されていない場合は、何もありませんreturn。その場合、メソッドはその定義で指定されたコントラクトを保証しないため、コンパイルできません。を返しますString

コメントまたは他の回答に従って、これをより適切に実行したり、読みやすくしたりできます ( switch-case)。

于 2013-09-06T18:24:34.700 に答える
0

これはうまくいくはずです:

public String getDayOfWeek(){
    if(numericDayOfWeek==0){
         return "Saturday";
    }
    else if(numericDayOfWeek==1){
        return "Sunday";
    }
    else if(numericDayOfWeek==2){
         return "Monday";
    }
    else if(numericDayOfWeek==3){
        return "Tuesday";
    }
    else if(numericDayOfWeek==4){
        return "Wednesday";
    }
    else if(numericDayOfWeek==5){
       return "Thursday";
    }
    else if(numericDayOfWeek==6){
        return "Friday";
    }
    else{
        return "Error";
    }
}
于 2013-09-06T18:35:09.530 に答える