Start New Discussion within our Software Development Community

I'm in an introductory programming class, and one of our assignments is to complete a Rock Paper Scissors program using nested switch statements

First of all, this method might be completely wrong in the first place, but currently every time I compile it it says I'm missing a return statement at the end, so I don't know what that's about either.
Any help would be appreciated. Thanks

/**
   *  returns -1 if the player wins,
   *  0 if it's a tie, and 1 if the computer wins
   */
  private int nextPlay(char computerMove, char playerMove)
  {
    switch(playerMove) 
    {
        case 'R':
            switch(computerMove)
            {
                case 'R':
                    return 0;
                case 'P':
                    return 1;   
                case 'S':
                    return -1;
            }
        break;

        case 'P':
            switch(computerMove)
            {
                case 'R':
                    return -1;
                case 'P':
                    return 0;
                case 'S':
                    return 1;
            }
        break;

        case 'S': 
            switch(computerMove)
            {
                case 'R':
                    return 1;
                case 'P':
                    return -1;
                case 'S':
                    return 0;
            }
        break;





    }   
  (MISSING RETURN STATEMENT) 
  }

That's the compiler being a bit smart. It's checking that you always return a value as specified in the method signature. Suppose your data is such that none of the cases is true... In that case it will drop through the switch and hit the end of the method without returning anything.
You need to add a return statement at the end of the method to handle that situation. It can return a value that indicates a problem, if you want.

This article has been dead for over six months. Start a new discussion instead.