作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我目前正在编写一个二十一点程序,该程序运行完全正常并给出正确的输出,除非添加了一张王牌并且该王牌使您的总分超过 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!");
}
}
}
谢谢!
最佳答案
问题出在这段代码中:
for(int x = 0; x <= total; x++) {
if ((count[x].getRank().getValue() == 11) && (frank >21))
frank = frank - 10;
break;
}
你在if
之外有break
。因此,您的 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
语句中使用大括号的示例。
关于java - 二十一点代码中涉及 A 的逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50111501/
我是一名优秀的程序员,十分优秀!