LvnLx
LvnLx

Reputation: 19

Missing Return Statement in Nested Switch Statements

I'm writing a section of code for a Rock, Paper, Scissors game. I am writing a method that returns 1, 0 or -1 depending on wether the computer wins, it's a tie, or the user wins, respectively. I have this code so far:

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;
        }
        case 'P': switch (computerMove) {
            case 'R': return -1;
            case 'P': return 0;
            case 'S': return 1;
        }
        case 'S': switch (computerMove) {
            case 'R': return 1;
            case 'P': return -1;
            case 'S': return 0;
        }
    }
}

It throws a "Missing Return Statement" at me at the last bracket. Any suggestions?

P.S. the only options available for both computerMove and playerMove are R, P and S!

Upvotes: 0

Views: 1203

Answers (3)

LvnLx
LvnLx

Reputation: 19

Thanks so much everyone! I decided to simply set one of the cases in each switch statement to default (keeping track of which char it is of course) and it resolved the issue.

I know this can cause some issues if the char is anything but the intended one, but my teacher says it is ok and I would use a different method (as suggested) next time!

Upvotes: 0

0xDEADBEEF
0xDEADBEEF

Reputation: 590

my suggestion

public static void main(String[] args)
        {
            Scanner scan = new Scanner(System.in);

            int x=nextPlay('R','P');
            System.out.println(x);
        }
        private static int nextPlay(char computerMove, char playerMove) 
        {
            if(playerMove=='R')
            {
                if(computerMove=='R')
                    return 0;
                else if(computerMove=='P')
                    return 1;
                else
                    return -1;
            }
            else if(playerMove=='P')
            {
                if(computerMove=='R')
                    return -1;
                else if(computerMove=='P')
                    return 0;
                else
                    return 1;
            }
            else
            {
                if(computerMove=='R')
                    return 1;
                else if(computerMove=='P')
                    return -1;
                else
                    return 0;
            }
        }

Upvotes: 0

Andreas
Andreas

Reputation: 159086

Others are telling you to add default to your switch statements. Not needed at all in this case, though it's a good general rule to follow.

However, you need to consider what should happen if playerMove and/or computerMove doesn't have one of the 3 expected values ('R', 'P', or 'S').

If computerMove doesn't, you'd want the logic flow to exit the outer switch statement, rather than fall through to the next case (though technically they'd all just fall through then, but still), so add a break in each outer case.

If that breaks out, or if playerMove doesn't have valid value, then logic flow gets to end of method, and there is no return statement there. That is your compilation error.

Best solution here, since you hopefully can't get into that situation, is to declare that to be exceptional, i.e. throw an Exception.

You code could be:

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;
    }
    throw new IllegalStateException("Oops! I messed up!!");
}

But it's better with more descriptive error messages:

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;
            }
            throw new IllegalArgumentException("Invalid computer move: " + computerMove);
        case 'P':
            switch (computerMove) {
                case 'R': return -1;
                case 'P': return 0;
                case 'S': return 1;
            }
            throw new IllegalArgumentException("Invalid computer move: " + computerMove);
        case 'S':
            switch (computerMove) {
                case 'R': return 1;
                case 'P': return -1;
                case 'S': return 0;
            }
            throw new IllegalArgumentException("Invalid computer move: " + computerMove);
    }
    throw new IllegalArgumentException("Invalid player move: " + playerMove);
}

Now, you could add those throw statements in a default clause instead. Same result.

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;
                default: throw new IllegalArgumentException("Invalid computer move: " + computerMove);
            }
        case 'P':
            switch (computerMove) {
                case 'R': return -1;
                case 'P': return 0;
                case 'S': return 1;
                default: throw new IllegalArgumentException("Invalid computer move: " + computerMove);
            }
        case 'S':
            switch (computerMove) {
                case 'R': return 1;
                case 'P': return -1;
                case 'S': return 0;
                default: throw new IllegalArgumentException("Invalid computer move: " + computerMove);
            }
        default: throw new IllegalArgumentException("Invalid player move: " + playerMove);
    }
}

Upvotes: 1

Related Questions