java - 我不明白为什么我的代码将 A 计为 11
问题描述
我必须为二十一点创建一个 Java 程序,而我所做的最后一部分是在必要时将 A 计为 1。但是由于某种原因,即使我使用了代码
if (deck.getCard(cardNumberInDeck).getValue().equals("Ace")){
if ((dealerHandValue += deck.toInt(cardNumberInDeck).getValueInt()) >21){
dealerHandValue += 1;
}
}
else {
dealerHandValue += deck.toInt(cardNumberInDeck).getValueInt();
cardNumberInDeck++;
}
(必要时将dealerHandValue 换成playerHandvalue) 我仍然明白,当我运行程序时,Aces 被计为11 并且破坏了获得它的人。
public class BlackJack {
public static void main(String[] args)
{
Deck deck = new Deck();
deck.shuffle();
Scanner input = new Scanner (System.in);
String a = "";
int hitstand;
int cardNumberInDeck = 0;
while (cardNumberInDeck < 52){
System.out.println("Do you want to play blackjack? (1) Yes (2) No");
int game = input.nextInt();
if (game == 2) {
break;
}
int playerHandValue = 0;
int dealerHandValue = 0;
System.out.println ("Your card is " + deck.getCard(cardNumberInDeck).toString() + " and " +
deck.getCard(cardNumberInDeck +1).toString()) ;
cardNumberInDeck+=2;
System.out.println ("Your Dealers cards are " + deck.getCard(cardNumberInDeck).toString() +
" and the other card is hidden");
cardNumberInDeck+=2;
playerHandValue = ((deck.toInt(cardNumberInDeck -4).getValueInt() + deck.toInt(cardNumberInDeck - 3).getValueInt()));
dealerHandValue = deck.toInt(cardNumberInDeck - 2).getValueInt();
if (playerHandValue == 22){
playerHandValue = 12;
}
if (dealerHandValue == 22){
dealerHandValue = 12;
}
System.out.println("Your hand value is " + playerHandValue);
System.out.println("The dealer's hand value is " + dealerHandValue + "+");
if (playerHandValue == 21){
System.out.println("You win, you have blackjack");
continue;
}
if ((dealerHandValue == 21)){
System.out.println("You Lose, the dealer has blackjack");
continue;
}
if (playerHandValue == 21 && dealerHandValue == 21){
System.out.println("You push, both you and the dealer have blackjacl");
continue;
}
boolean g = true;
boolean t = true;
int ticker = 0;
while(g == true){
System.out.println("Would you like to hit(1) or stand(2):");
hitstand = input.nextInt();
if (hitstand == 1){
System.out.println("Your hit and get a " + deck.getCard(cardNumberInDeck).toString());
if (deck.getCard(cardNumberInDeck).getValue().equals("Ace")){
if ((playerHandValue += deck.toInt(cardNumberInDeck).getValueInt()) >21){
playerHandValue += 1;
}
}
else {
playerHandValue += deck.toInt(cardNumberInDeck).getValueInt();
cardNumberInDeck++;
}
System.out.println("Your hand value is " + playerHandValue);
if (playerHandValue > 21) {
System.out.println("You lost, because you busted ");
g = false;
if (cardNumberInDeck > 45){
System.out.println("The deck has to few cards. Reshuffling cards");
deck.shuffle();
cardNumberInDeck = 0;
}
}
ticker++;
}
else {
System.out.println("The dealers hand is " + deck.getCard(cardNumberInDeck - (2 + ticker)).toString() + " and " +
deck.getCard(cardNumberInDeck - (1 + ticker)).toString());
dealerHandValue += deck.toInt(cardNumberInDeck - (1 + ticker)).getValueInt();
while (dealerHandValue < 17){
System.out.println("The dealer hits and gets a " + deck.getCard(cardNumberInDeck).toString() + ". ");
if (deck.getCard(cardNumberInDeck).getValue().equals("Ace")){
if ((dealerHandValue += deck.toInt(cardNumberInDeck).getValueInt()) >21){
dealerHandValue += 1;
}
}
else {
dealerHandValue += deck.toInt(cardNumberInDeck).getValueInt();
cardNumberInDeck++;
}
cardNumberInDeck++;
if (dealerHandValue > 21){
System.out.println("The dealer's hand value is: " + dealerHandValue);
System.out.println("The dealer busted, you win");
t = false;
g = false;
if (cardNumberInDeck > 45){
System.out.println("The deck has to few cards. Reshuffling cards");
deck.shuffle();
cardNumberInDeck = 0;
}
}
}
if (t == true){
if (dealerHandValue < playerHandValue){
System.out.println("Your hand value was " + playerHandValue + " the dealer's hand value is " +
dealerHandValue);
System.out.println("You win, your hand was better");
g = false;
if (cardNumberInDeck >45){
System.out.println("The deck has to few cards. Reshuffling cards");
deck.shuffle();
cardNumberInDeck = 0;
}
}
else if(dealerHandValue > playerHandValue){
System.out.println("Your hand value was " + playerHandValue + " the dealer's hand value is " +
dealerHandValue);
System.out.println("You lose, your hand was worse");
g = false;
if (cardNumberInDeck > 45){
System.out.println("The deck has to few cards. Reshuffling cards");
deck.shuffle();
cardNumberInDeck = 0;
}
}
else if (dealerHandValue == playerHandValue){
System.out.println("Your hand value was " + playerHandValue + " the dealer's hand value is " +
dealerHandValue);
System.out.println("You push, your hands were equal");
g = false;
if (cardNumberInDeck > 45){
System.out.println("The deck has to few cards. Reshuffling cards");
deck.shuffle();
cardNumberInDeck = 0;
}
}
}
}
}
}
}
}
解决方案
正如评论中指出的那样,if ((dealerHandValue += deck.toInt(cardNumberInDeck).getValueInt()) >21)
将添加和分配。
利用:
if ((dealerHandValue + deck.toInt(cardNumberInDeck).getValueInt()) >21){
dealerHandValue += 1;
}
替换+=
为+
只会返回两个变量的总和,而不分配(和更改)原始值。x += y
是 的简写形式x = x + y
。
推荐阅读
- org-mode - 在 orgmode ASCII 导出中,省略列表中项目之间的空白行
- java - Swing:如何在每个组件、JPanel、JButton 等上绘制动画?
- neural-network - 编码器-解码器 LSTMCell/RNN 网络的输入
- fiddler - Fiddler 未捕获来自特定主机的流量
- php - 我想访问此页面的关注者,但它只显示这么多数据
- python - 在 Keras 中使用迁移学习训练 CNN - 图像输入不起作用,但矢量输入起作用
- struts2 - 需要将 Struts 2.3.24.1 更新到 Struts 最新版本,即 2.5.20
- php - PHP:MySQL 数据库表中的更新不起作用
- javascript - 使用 JQuery 获取单元格值
- android - 模拟器在 Mac 上的 Android Studio 上暂时显示