首页 > 解决方案 > 二十一点代码中涉及 ​​A 的逻辑错误

问题描述

所以我目前正在编写一个二十一点程序,该程序运行完全正常并给出正确的输出,除非添加一个 ace 并且该 ace 让你的总分超过 21。这不仅发生在用户的手上,也发生在庄家的手上。我很确定错误发生在我创建 countTotal 的第一个方法中。这是二十一点课程:

public class Blackjack {

    static int total = 0;
    static int deal = 0;

    public static int countTotal(Card[] count) {
        int frank = 0;

        for(int i = 0; i < total; i++) 
        frank = count[i].getRank().getValue() + frank;  
        if (frank > 21) {
            for(int x = 0; x <= total; x++) {
                if ((count[x].getRank().getValue() == 11) && (frank >21)) 
                    frank = frank - 10; break;
            }
        }
        deal = frank;
        return frank;
    }

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

        CardDeck bjack = new CardDeck();
        bjack.shuffle();

        Card[] myCards = new Card[11];
        myCards[0] = bjack.nextCard();
        myCards[1] = bjack.nextCard();
        total = 2;

        Card[] dealCards = new Card[11];
        dealCards[0] = bjack.nextCard();
        dealCards[1] = bjack.nextCard();

        System.out.println("Your starting cards are " + myCards[0] + " and " + myCards[1] + 
                " for a total of " + countTotal(myCards));

        System.out.println("\nThe dealer's starting card is " + dealCards[0]);

        System.out.print("\n\nType 0 to stay and 1 to hit. --->     ");
        int hit = scan.nextInt();



        for(int i = 2; hit == 1; i++){
            myCards[i] = bjack.nextCard();      
            total++;

            System.out.print("Your cards are " );
            for (int j = 0; j < total; j++) {
                System.out.print(myCards[j] + " ");
                if (j + 2 == total)
                    System.out.print("and ");
                if (j + 1 == total)
                    System.out.print("for a total of " + countTotal(myCards));
            }
            System.out.println("");

            if (countTotal(myCards) < 21) {
            System.out.print("\nWould you like to hit again? Type 0 to stay and 1 to hit --->    ");
            hit = scan.nextInt();
            }
            else if (countTotal(myCards) == 21) {
                System.out.println("\nCongratulations you are at 21!");
                hit = 0;
            }
            else {
                System.out.println("\nYou went over 21 and lost!");
                hit = 0;
            }
        }
        myTotal = total;
        total = 2;
        if (countTotal(myCards) <= 21) {
        System.out.println("\n\nThe dealer's cards are " +  dealCards[0] + " and " + dealCards[1] + " for a total of " + countTotal(dealCards));

        for (int i = 2; countTotal(dealCards) < 17; i++) {
            dealCards[i] = bjack.nextCard();
            total++;
            System.out.print("\nThe dealer's cards are " );
            for (int j = 0; j < total; j++) {
                System.out.print(dealCards[j] + " ");
                if (j + 2 == total)
                    System.out.print("and ");
                if (j + 1 == total)
                    System.out.print("for a total of " + countTotal(dealCards) + "\n");
                }
            }
        deal = countTotal(dealCards);
        total = myTotal;
        int my = countTotal(myCards);

        if ((my > deal) && (my < 22))
            System.out.println("\n\nCongratulations you beat the dealer in blackjack!");
        else if (my == deal)
            System.out.println("\n\nYou and the dealer tied. Good game!");
        else if ((deal > my) && (deal < 22))
            System.out.println("\n\nThe dealer beat you, Nice try!");
        else
            System.out.println("\n\nCongratulations you beat the dealer in blackjack!");
        }
    }
}

谢谢!

标签: java

解决方案


问题出在这段代码中:

for(int x = 0; x <= total; x++) {
  if ((count[x].getRank().getValue() == 11) && (frank >21)) 
    frank = frank - 10; 
  break;
}

你有break外面的if。因此,您的for循环始终只执行 1 次。您检查count[0]它是否等于 11(不是),然后立即跳出循环。更改代码如下:

for(int x = 0; x <= total; x++) {
  if ((count[x].getRank().getValue() == 11) && (frank >21)) {
    frank = frank - 10; 
    break;
  }
}

这就是为什么您必须始终在if语句中使用大括号的示例。


推荐阅读