【问题标题】:java: missing return statement for blackjack gamejava:二十一点游戏缺少返回语句
【发布时间】:2020-11-07 03:08:48
【问题描述】:
public static String determiner(String userResponse, int playerHand, int AiHand) {
    if (userResponse.equals("hit") && playerHand < 21 && AiHand <21) {
        int RandomGen = (int) (Math.random()*11 + 1);
        int newHand = playerHand + RandomGen;
        int AIRandomGen = (int) (Math.random()*11 + 1);
        int newAIHand = AiHand + AIRandomGen;
        return "Your current hand is " + newHand;
    }
    else if (userResponse.equals("stay") && playerHand > 21 && AiHand < 21) {
        if (playerHand > AiHand) {
             String x = "You won!";
             return x;
        }
        else if (AiHand > playerHand) {
            String x =  "Sorry, you lost.";
            return x;
        }
        else if (playerHand == 21) {
            String x = "You won! You hit the jackpot :0";
            return x;
        }
        else if (AiHand == 21) {
            String x = "Sorry, you lost. Enemy hit the jackpot";
            return x;
        }
    }
    else if (userResponse.equals("hit") && playerHand > 21) {
        String x =  "Game over, you lost. You went boom boom";
        return x;
    }
    else {
        return null;
    }
}

我查看了其他问题,但它们似乎没有帮助。我不知道我的代码有什么问题。有人能帮我吗?这应该是一个检查玩家的手牌是否超过 21 点之类的功能(二十一点游戏)。

【问题讨论】:

    标签: java return


    【解决方案1】:
    else if (AiHand == 21) {
            String x = "Sorry, you lost. Enemy hit the jackpot";
            return x;
    }
    

    想象一下,如果 AiHand 在这里不是 21 岁会怎样:它会一直持续到最后而不返回任何内容。

    修复此else if

    【讨论】:

    • 但是,我没有一个 else 语句会要求它返回 null 吗?编辑:哦等等,我明白了。
    猜你喜欢
    • 2014-10-22
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 2013-10-08
    • 2013-04-29
    • 2023-03-21
    • 2020-05-18
    • 1970-01-01
    相关资源
    最近更新 更多