二十一点程序总是强迫用户 'Hit' 即使他们说 'Stay'

Blackjack Program Always forcing user to 'Hit' even if they say 'Stay'

所以我正在制作一个二十一点程序。除了一个错误类型的东西外,我几乎完全成功地制作了游戏。用户可以选择 'hit' 或 'stay',就像您通常在 21 点中所做的那样,但是当它最后告诉他们他们的总数时,即使他们说要停留两次,它也会添加 2 'hits'。例如,如果我得到一个 4 和一个 6,总共 10。那么我只停留两次以保留 10。程序无论如何都会再滚动 2 个数字,最后会说总数像 20 而不是我最初的 10得到。如果需要,您可以 运行 我的程序查看更多内容,这是代码;

/////////////////////////////////////
// Name: Mackenzie Cutler
// Class: CP 12
// Date: March 28th, 2018
/////////////////////////////////////

import java.util.Scanner;
import java.util.Random;
public class MCproject3
{
  public static void main(String[] args)
  {
    Scanner k = new Scanner(System.in);
    Random ran = new Random();

    //Welcoming user & choosing their initial cards
    System.out.println("Welcome to the Cutler Casino Program. Currently playing Blackjack!");
    int a1 = ran.nextInt(11) + 1;
    int a2 = ran.nextInt(10) + 1;
    int a3 = ran.nextInt(11) + 1;
    int a4 = ran.nextInt(11) + 1;
    System.out.println ("\nYou get a " + a1 + " and a " + a2);
    System.out.println ("Your total is " + (a1+a2));

    //Choosing dealers initial cards and telling user
    int b1 = ran.nextInt(11) + 1;
    int b2 = ran.nextInt(10) + 1;
    int b3 = ran.nextInt(11) + 1;
    int b4 = ran.nextInt(11) + 1;
    System.out.println("\nThe dealer has a " + b1 + " showing, and a hidden card.");
    System.out.println("His total is hidden, too.");

    //User chooses to 'Hit' or 'Stay'
    System.out.print("\nWould you like to 'Hit' or 'Stay'?");
    String choice = k.nextLine();
    if(choice.equalsIgnoreCase ("hit"))
    {
      System.out.println("You drew a " + a3);
      System.out.println("Your total is " + (a1+a2+a3));
      if(a1+a2+a3 > 21)
      {
        System.out.println("You busted! Since you exceeded 21 the dealer wins, sorry.");
        return;
      }     
    }
    else if(choice.equalsIgnoreCase ("stay"))
    {
      System.out.println(" ");
    }
    else
    {
      System.out.println("Error. Make sure you typed either 'Stay' or 'Hit'. Please re-run the program :)");
    }

    //Second time user chooses to 'Hit' or 'Stay'
    System.out.print("\nWould you like to 'Hit' or 'Stay'?");
    String choice2 = k.nextLine();
    if(choice2.equalsIgnoreCase ("hit"))
    {
      System.out.println("You drew a " + a4);
      System.out.println("Your total is " + (a1+a2+a3+a4));
      if(a1+a2+a3+a4 > 21)
      {
        System.out.println("You busted! Since you exceeded 21 the dealer wins, sorry.");
        return;
      }     
    }
    else if(choice2.equalsIgnoreCase ("stay"))
    {
      System.out.println(" ");
    }
    else
    {
      System.out.println("Error. Make sure you typed either 'Stay' or 'Hit'. Please re-run the program :)");
    }

    //Dealers reveal and is his turn to choose 'Hit' and 'Stay'
    System.out.println("\nOkay, Dealers turn.");
    System.out.println("His hidden card was " + b2);
    System.out.println("His total was " + (b1+b2));
    int dchoice = ran.nextInt(2) + 1;

    if(dchoice == 1)
    {
      System.out.println("\nDealder chooses to hit.");
      System.out.println("He draws a " + b3);
      System.out.println("His total is now " + (b1+b2+b3));
      if(b1+b2+b3 > 21)
      {
        System.out.println("Dealer busted! Since he exceeded 21 you WIN!!");
        return;
      }     
    }
    else if(dchoice == 2)
    {
      System.out.println("\nDealer chooses to stay.");
    }
    else
    {
      System.out.println("Error 404. Program Failed, We are sorry. Please restart.");
    }

    //Dealers second 'Hit' or 'Stay' random choice
    int dchoice2 = ran.nextInt(2) + 1;
    if(dchoice2 == 1)
    {
      System.out.println("\nDealder chooses to hit.");
      System.out.println("He draws a " + b4);
      System.out.println("His total is now " + (b1+b2+b3+b4));
      if(b1+b2+b3+b4 > 21)
      {
        System.out.println("Dealer busted! Since he exceeded 21 you WIN!!");
        return;
      } 
    }
    else if(dchoice == 2)
    {
      System.out.println("\nDealer chooses to stay.");
    }
    else
    {
      System.out.println(" ");
    }


    //Ending
    int totala = (a1+a2+a3+a4);
    int totalb = (b1+b2+b3+b4);

    System.out.println("\nDealers total is " + (b1+b2+b3+b4));
    System.out.println("Your total is " + (a1+a2+a3+a4));
    if(totala > totalb)
    {
      if(totala <= 21)
      {
      System.out.println("\nYou WIN!");
      }
      else if(totala > 21)
      {
        System.out.println("\nYou busted so you wont win :(");
      }
    }
    else if(totala < totalb)
    {
      if(totalb <= 21)
      {
        System.out.println("\nSorry, Dealer Wins.");
      }
      else if(totalb > 21)
      {
        System.out.println("Dealer busted so you win!");
      }
    }
    else
    {
      System.out.println("\nError 405. Program Failed, We are sorry. Please restart.");
    }
  }
}

我只是想知道您是否认为我做错了什么或应该采取不同的措施以使其正常工作。

问题很简单,但又难以捉摸

您已经显示了第一个总数 a1+a2。但是,在显示结果时,您显示 a1+a2+a3+a4。现在,a3 和 a4 已经被初始化为一些随机数,因此最终的结果总是会比初始的多,即使你 'stay' 两次。

解决方案-

  1. 要解决此问题,您可以创建一个名为 int UserTotal = a1+a2 的变量,如果用户是第一次 'hits',则向其添加 a3,向其添加 a4如果用户 'hits' 第二次。然后,将 totala 初始化为 UserInput.
  2. 或者,你可以在程序一开始就将a3a4设置为0,如果用户是第一次'hits',你可以设置a3为一个随机值,如果用户点击第二次,a4可以设置为一个随机值。

您总是这样计算总数:

int totala = (a1+a2+a3+a4);
int totalb = (b1+b2+b3+b4);

这些变量 a1a2 等在您的 main 方法一开始就被赋值,如下所示:

int a1 = ran.nextInt(11) + 1;
int a2 = ran.nextInt(10) + 1; // is this correct?
int a3 = ran.nextInt(11) + 1;
int a4 = ran.nextInt(11) + 1;

所以无论用户输入什么,它总是计算4个值的总和作为总数。

要解决此问题,请给 a3a4 初始值 0,并且仅在它们 "hit".

时才赋值

另一种方法是将数字存储在 List 中,这样总和就是 List 中整数的总和,例如

List<Integer> playerCards = new ArrayList<>();
// Add initial cards...
playerCards.add(ran.nextInt(11) + 1);
playerCards.add(ran.nextInt(11) + 1);

if (playerHit) { // for example...
    playerCards.add(ran.nextInt(11) + 1);
}

// Calculate player total
int playerTotal = 0;
for (int cardValue : playerCards) {
    playerTotal += cardValue;
}
System.out.println("Player total is: " + playerTotal);

此外,您的代码有很多重复,您应该考虑将其重构为更小的可重用方法。

你只需要在下面设置这两个变量,当它们真正决定命中时而不是在开始时自动。否则你希望这两个变量在开始时为零,这样当它们没有命中时你添加所有你得到正确结果的东西!

int a3 = ran.nextInt(11) + 1;
int a4 = ran.nextInt(11) + 1;