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

5

Sotirios 是正确的,但这里更好的解决方案是使用 case 语句:

switch(numericDayOfWeek)
{
    case 0:
      return "Saturday";
    case 1:
      return "Sunday";
    case 2:
      return "Monday";
    case 3:
      return "Tuesday";
    case 4:
      return "Wednesday";
    case 5:
      return "Thursday";
    case 6:
      return "Friday";
    default:
      return "Error";
}
于 2013-09-06T18:27:23.050 回答
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

编译器错误的原因是编译器无法确定您的代码将始终从您的方法返回一个字符串。

如果 numericDayOfWeek 不在 0 到 6 的范围内,则您的函数不会指定应返回的值,并且编译器无法知道或保证 numericDayOfWeek 将始终在所需的范围内。

return不幸的是,即使在简单的情况下,编译器在确保语句的能力方面也受到限制。采取以下琐碎(且无用)的方法:

// I have a compiler error!
public boolean testReturn()
{
    final boolean condition = true;

    if (condition) return true;

    if (!condition) return false;
}

以上将导致编译器错误,说明该方法必须返回布尔类型。我们可以通过将第二条if语句更改为else子句来轻松修复它,因为这是允许编译器确保保证执行一个或其他代码块的少数方法之一。

// I compile!
public boolean testReturn()
{
    final boolean condition = true;

    if (condition) return true
    else return false;
}

规则是具有返回类型的方法不能正常完成,而必须按照JLS 8.4.7突然完成(此处突然通过return语句或异常表示)。编译器根据JLS 14.21 Unreachable Statements中定义的规则查看是否可以正常终止,因为它还定义了正常完成的规则​​。

对于您的具体示例,我建议考虑将 an作为方法的最后一行并用语句IllegalArgumentException替换您的语句。例如ifswitch

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

    throw new IllegalArgumentException("numericDayOfWeek is out of range: " + numericDayOfWeek);
}

您也可以在语句的default子句中抛出异常switch,但在这种情况下,我会说这只是个人喜好问题,我更喜欢在switch此处之外。

于 2013-09-06T23:08:10.993 回答
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 回答