Error “this method must return a result of type int”?

后端 未结 6 2054
攒了一身酷
攒了一身酷 2021-01-28 03:06

I have this code below and it keep telling me that thanksgiving() must return a result type of int. I have casted all the results just to make sure, but nothing seems to be work

相关标签:
6条回答
  • 2021-01-28 03:19

    All the returns are in if statements, in this case, compiler sees that there might be a situation in which the function won't return anything. You need to add one return outside any ifs or in an else statement.

    0 讨论(0)
  • 2021-01-28 03:21

    You need something like this

    public static int thanksgiving(int year)
    {
        int day = firstOfMonth( year );
        if ( day == THURS )
        {
            return (int) 22;
        } else if ( day > THURS )
        {
            return (int) 29 - ( day - THURS );
        } else {
            return (int) 22 + ( THURS + day );
        }
    }
    
    0 讨论(0)
  • 2021-01-28 03:23

    In this code:

    public static int thanksgiving(int year)
    {
      int day = firstOfMonth( year );
      if ( day == THURS ) 
      {
        return (int) 22;
      }
      if ( day > THURS )
      {
        return (int) 29 - ( day - THURS );
      }
      if ( day < THURS )
      {
        return (int) 22 + ( THURS + day );
      }
    }
    

    You're making your return statement in several if blocks. What if none of them are true? The compiler will not allow this, and you should either return a default value at the bottom or throw an exception. Or else make some of the if statements else - if with a last else:

    public static int thanksgiving(int year){
      int day = firstOfMonth( year );
      if ( day == THURS ) {
        return (22;
      } else if ( day > THURS ) {
        return 29 - ( day - THURS );
      } else { // else without the if
        // we know that ( day < THURS )
        return 22 + ( THURS + day );
      }
    }
    

    Also:

    • There is no need to cast an int into an int.
    • An unrelated issue is that your code uses a lot of "magic" numbers and you will want to avoid using them.
    0 讨论(0)
  • 2021-01-28 03:34

    You have 3 if statements inside thanksgiving() method. What if none of the condition gets satisified? What would return in that case? So compiler is complaining.

    0 讨论(0)
  • 2021-01-28 03:38

    The problem is that all of your return statements are within an if statement. If, for some reason, day != Thursday, not < thursday, and not > thursday (I know this is impossible), then nothing will be returned. Change your code to have a default return value:

    public static int thanksgiving(int year)
    {
      int day = firstOfMonth( year );
      if ( day == THURS ) 
      {
        return (int) 22;
      }
      else if ( day > THURS )
      {
        return (int) 29 - ( day - THURS );
      }
      else 
      {
        return (int) 22 + ( THURS + day );
      }
    }
    
    0 讨论(0)
  • 2021-01-28 03:45

    The Java compiler is too stupid to realize that in every situation, one of your conditions will be satisfied. It sees the if statements and thinks that it is possible that none of them will be satisfied. You can add a return at the end of the method that indicates an error case, you can throw an exception, or you can assert false.

    0 讨论(0)
提交回复
热议问题