在二十一点代码中涉及Aces的逻辑错误

时间:2018-05-01 04:55:36

标签: java

所以我现在正在编写一个二十一点程序,程序运行完全正常并提供正确的输出,除非添加一个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!");
        }
    }
}

谢谢!

1 个答案:

答案 0 :(得分:1)

问题在于此代码:

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

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

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

这就是为什么你必须总是使用带有if语句的大括号的例子。