为什么这个公共字符串函数不能工作?

时间:2022-09-18 12:39:45

It is saying that it must return a string but I don't see anything wrong with it? I think numericDayOfWeek should be working fine?

它的意思是它必须返回一个字符串,但我看不出它有什么问题吗?我想每周的第一天应该没问题吧?

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

Here is the full code

这是完整的代码

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 个解决方案

#1


5  

Sotirios is correct, but a better solution here would be to use a case statement:

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

#2


3  

If none of the conditions passes, ie. they all evaluate to false, the method wouldn't return anything. Add a default return at the end

如果没有任何条件通过的话。它们都取值为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";
 }

The compiler considers all paths. If none if the if statements was executed it wouldn't have anything to return. In that case, it won't be able to compile because the method wouldn't guarantee the contract specified by its definition, ie. to return a String.

编译器考虑所有路径。如果没有,如果执行If语句,它就不会返回任何内容。在这种情况下,它将无法编译,因为该方法不能保证其定义所指定的契约。返回一个字符串。

Follow the comments or the other answer on how to possibly makes this perform better or make it easier to read (switch-case).

根据评论或其他答案,如何使其更好地执行或使其更容易阅读(切换情况)。

#3


0  

This should work:

这应该工作:

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

#4


0  

The reason for the compiler error is that the compiler cannot be certain that your code will always return a String from your method.

编译器错误的原因是编译器不能确定您的代码总是从您的方法返回一个字符串。

In the event that numericDayOfWeek is not in the range of 0 to 6, your function does not specify what value should be returned, and there is no way for the compiler to know or guarantee that numericDayOfWeek will always be within the desired range.

如果numericDayOfWeek不在0到6的范围内,那么函数不会指定应该返回的值,编译器也无法知道或保证numericDayOfWeek始终在期望的范围内。

Unfortunately, the compiler is limited in its ability to ensure a return statement even in simple cases. Take the trivial (and useless) method below:

不幸的是,即使在简单的情况下,编译器在确保返回语句的能力上也是有限的。下面是简单(和无用)的方法:

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

    if (condition) return true;

    if (!condition) return false;
}

The above will result in a compiler error saying the method must return a type of boolean. We could easily fix it by changing the second if statement to be an else clause since that is one of the few ways that allow the compiler to ensure one or the other blocks of code are guaranteed to be executed.

上述结果将导致编译器错误,表示该方法必须返回一个布尔值类型。我们可以通过将第二个if语句改为else子句来轻松解决它,因为这是允许编译器确保一个或其他代码块被执行的少数方法之一。

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

    if (condition) return true
    else return false;
}

The rules are that a method with a return type must not complete normally and must instead complete abruptly (abruptly here indicating via a return statement or an exception) per JLS 8.4.7. The compiler looks to see whether normal termination is possible based on the rules defined in JLS 14.21 Unreachable Statements as it also defines the rules for normal completion.

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

In the case of your specific example, I would suggest considering throwing an IllegalArgumentException as the last line of your method and replacing your if statement with a switch statement. E.g.

对于特定的示例,我建议将一个IllegalArgumentException作为方法的最后一行,并使用switch语句替换if语句。如。

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

You can also throw the exception in a default clause of the switch statement, but in this case I would say that is just a matter of personal preference and I prefer outside of the switch here.

你也可以在switch语句的默认子句中抛出异常,但在本例中,我认为这只是个人偏好的问题,而我更喜欢在这里进行切换。

#1


5  

Sotirios is correct, but a better solution here would be to use a case statement:

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

#2


3  

If none of the conditions passes, ie. they all evaluate to false, the method wouldn't return anything. Add a default return at the end

如果没有任何条件通过的话。它们都取值为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";
 }

The compiler considers all paths. If none if the if statements was executed it wouldn't have anything to return. In that case, it won't be able to compile because the method wouldn't guarantee the contract specified by its definition, ie. to return a String.

编译器考虑所有路径。如果没有,如果执行If语句,它就不会返回任何内容。在这种情况下,它将无法编译,因为该方法不能保证其定义所指定的契约。返回一个字符串。

Follow the comments or the other answer on how to possibly makes this perform better or make it easier to read (switch-case).

根据评论或其他答案,如何使其更好地执行或使其更容易阅读(切换情况)。

#3


0  

This should work:

这应该工作:

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

#4


0  

The reason for the compiler error is that the compiler cannot be certain that your code will always return a String from your method.

编译器错误的原因是编译器不能确定您的代码总是从您的方法返回一个字符串。

In the event that numericDayOfWeek is not in the range of 0 to 6, your function does not specify what value should be returned, and there is no way for the compiler to know or guarantee that numericDayOfWeek will always be within the desired range.

如果numericDayOfWeek不在0到6的范围内,那么函数不会指定应该返回的值,编译器也无法知道或保证numericDayOfWeek始终在期望的范围内。

Unfortunately, the compiler is limited in its ability to ensure a return statement even in simple cases. Take the trivial (and useless) method below:

不幸的是,即使在简单的情况下,编译器在确保返回语句的能力上也是有限的。下面是简单(和无用)的方法:

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

    if (condition) return true;

    if (!condition) return false;
}

The above will result in a compiler error saying the method must return a type of boolean. We could easily fix it by changing the second if statement to be an else clause since that is one of the few ways that allow the compiler to ensure one or the other blocks of code are guaranteed to be executed.

上述结果将导致编译器错误,表示该方法必须返回一个布尔值类型。我们可以通过将第二个if语句改为else子句来轻松解决它,因为这是允许编译器确保一个或其他代码块被执行的少数方法之一。

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

    if (condition) return true
    else return false;
}

The rules are that a method with a return type must not complete normally and must instead complete abruptly (abruptly here indicating via a return statement or an exception) per JLS 8.4.7. The compiler looks to see whether normal termination is possible based on the rules defined in JLS 14.21 Unreachable Statements as it also defines the rules for normal completion.

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

In the case of your specific example, I would suggest considering throwing an IllegalArgumentException as the last line of your method and replacing your if statement with a switch statement. E.g.

对于特定的示例,我建议将一个IllegalArgumentException作为方法的最后一行,并使用switch语句替换if语句。如。

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

You can also throw the exception in a default clause of the switch statement, but in this case I would say that is just a matter of personal preference and I prefer outside of the switch here.

你也可以在switch语句的默认子句中抛出异常,但在本例中,我认为这只是个人偏好的问题,而我更喜欢在这里进行切换。