为什么这个公共String函数不起作用?

时间:2013-09-06 18:23:57

标签: java string function methods 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 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 个答案:

答案 0 :(得分:5)

Sotirios是正确的,但这里更好的解决方案是使用案例陈述:

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";
}

答案 1 :(得分: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)。

答案 2 :(得分: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";
    }
}

答案 3 :(得分:0)

编译器错误的原因是编译器无法确定您的代码是否总是从您的方法返回一个String。

如果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;
}

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

对于您的具体示例,我建议您考虑将IllegalArgumentException作为方法的最后一行,并将if语句替换为switch语句。 E.g。

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子句中抛出异常,但在这种情况下,我会说这只是个人偏好的问题,我更喜欢在{{1}之外这里。