简体   繁体   English

嵌套切换语句中缺少返回语句

[英]Missing Return Statement in Nested Switch Statements

I'm writing a section of code for a Rock, Paper, Scissors game. 我正在为Rock,Paper,Scissors游戏编写一段代码。 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. 我正在编写一种方法,该方法分别根据计算机获胜,平局还是用户获胜而返回1、0或-1。 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? 有什么建议么?

PS the only options available for both computerMove and playerMove are R, P and S! PS PS仅适用于computerMove和playerMove的选项是R,P和S!

Others are telling you to add default to your switch statements. 其他人则告诉您将default添加到switch语句中。 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' ). 但是,您需要考虑如果playerMove和/或computerMove没有3个期望值( 'R''P''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 . 如果computerMove不这样做,则您希望逻辑流程退出外部switch语句,而不是进入下一个case (尽管从技术上讲,它们都只是到那时为止,但仍然如此),因此请在每个情况中添加一个breakcase

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. 如果发生这种情况,或者playerMove没有有效值,则逻辑流程到达方法的结尾,并且那里没有return语句。 That is your compilation error. 是你的编译错误。

Best solution here, since you hopefully can't get into that situation, is to declare that to be exceptional , ie throw an Exception . 最好的解决方法是,声明您是例外情况 ,即throw 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. 现在,您可以将这些throw语句添加到default子句中。 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);
    }
}

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

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. 我决定简单地将每个switch语句中的一种情况设置为默认值(当然跟踪它是哪个字符),它解决了该问题。

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! 我知道,如果char不是预期的字符,可能会导致一些问题,但是我的老师说这没问题,下次我会使用其他方法(如建议的那样)!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM