首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java 21点计分问题

Java 21点计分问题
EN

Stack Overflow用户
提问于 2018-03-14 23:47:04
回答 2查看 99关注 0票数 0

我在我的二十一点游戏中得分有问题。它可以找到正确的分数,但当用户抽出一张新卡片时,它会错误地添加分数。

例如:原始手势是:4和5(因此分数为9),用户画出的是10,而不是19,而是19+9或28。

下面是我的代码:评分方法:

代码语言:javascript
复制
public int getHandValue() {
    boolean ace = false;
    for (int i = 0; i < this.hand.size(); i++) {
        if (this.hand.get(i).getRank().value > 10) {
            points += 10;
        } else if (this.hand.get(i).getRank().value == 1) {
            ace = true;
        } else {
            points += this.hand.get(i).getRank().value;
        }
        if (ace == true && points + 11 <= 21) {
            points += 11;
        }

    }
    return points;
}

播放方法:

代码语言:javascript
复制
public void play(Deck deck) {
    boolean isDone = false;
    if (this.getHandValue() > 21){
        System.out.println("You have busted!");
        isDone = true;
        this.lose();
    }
    takeCard(deck.drawCard());
    takeCard(deck.drawCard());
    System.out.println("Here are your cards and your score:");
    System.out.println(this.hand.toString());
    System.out.println("Score: " + getHandValue());
    ListItemInput hitOrPass = new ListItemInput();
    hitOrPass.add("h", "hit");
    hitOrPass.add("p", "pass");
    while (!isDone){
        System.out.println("Hit or pass?");
        hitOrPass.run();
        if (hitOrPass.getKey().equalsIgnoreCase("h")) {
            String result = "";
            this.takeCard(deck.drawCard());
            result += "You hand is now " + this.hand.toString() + "\n";
            result += "Your score is now " + this.getHandValue();
            System.out.println(result);
        } else {
            System.out.println("You have chosen to pass.");
            isDone = true;
        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2018-03-14 23:51:11

每次调用你的方法时,你都会循环遍历手部,所以在这样做之前,你的点应该被重置。否则,点数将增加2倍+手中额外的卡片。在循环手部之前重置该值

代码语言:javascript
复制
public int getHandValue() {
    boolean ace = false;
    points = 0; //<--- reset the point total
    for (int i = 0; i < this.hand.size(); i++) {
        if (this.hand.get(i).getRank().value > 10) {
            points += 10;
        } else if (this.hand.get(i).getRank().value == 1) {
            ace = true;
        } else {
            points += this.hand.get(i).getRank().value;
        }
        if (ace == true && points + 11 <= 21) {
            points += 11;
        }

    }
    return points;
票数 1
EN

Stack Overflow用户

发布于 2018-03-14 23:53:15

我假设points是在这个方法之外声明的。

因为你要返回点,所以最好不要使用类范围的变量。你最终会得到像这样意想不到的结果。相反,在方法范围内使用变量,如下所示。

代码语言:javascript
复制
public int getHandValue() {
    boolean ace = false;
    int value = 0;

    for (int i = 0; i < this.hand.size(); i++) {
        if (this.hand.get(i).getRank().value > 10) {
            value += 10;
        } else if (this.hand.get(i).getRank().value == 1) {
            ace = true;
        } else {
            value += this.hand.get(i).getRank().value;
        }
        if (ace == true && points + 11 <= 21) {
            value += 11;
        }
    }

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

https://stackoverflow.com/questions/49282162

复制
相关文章

相似问题

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