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

后端 未结 6 2159
攒了一身酷
攒了一身酷 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: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 );
      }
    }
    

提交回复
热议问题