首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java:缺少21点游戏的返回语句

java:缺少21点游戏的返回语句
EN

Stack Overflow用户
提问于 2020-11-07 11:08:49
回答 1查看 84关注 0票数 0
代码语言:javascript
复制
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;
    }
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-07 11:15:25

代码语言:javascript
复制
else if (AiHand == 21) {
        String x = "Sorry, you lost. Enemy hit the jackpot";
        return x;
}

想象一下,如果AiHand在这里不是21 :它将继续到最后而不返回任何内容。

修复此else if

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64724061

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档