【问题标题】:Blackjack Program Always forcing user to 'Hit' even if they say 'Stay'二十一点计划总是强迫用户“打”,即使他们说“留下”
【发布时间】:2018-09-13 01:35:36
【问题描述】:

所以我正在制作一个二十一点程序。除了一个错误类型的东西外,我几乎完全成功地制作了游戏。用户可以像通常在二十一点中那样选择“命中”或“停留”,但是当它最终告诉他们他们的总数时,即使他们说停留两次,它也会添加 2 个“命中”。例如,如果我得到一个 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.");
    }
  }
}

我只是想知道你是否认为我做错了什么或者应该做不同的事情以使其正常工作。

【问题讨论】:

    标签: java blackjack


    【解决方案1】:

    这个问题很简单却又难以捉摸。

    您已显示a1+a2 的第一个总数。但是,在显示结果时,您会显示 a1+a2+a3+a4。现在,a3 和 a4 已经初始化为一些随机数,因此即使您“停留”两次,最终结果也将始终大于初始结果。

    解决方案-

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

    【讨论】:

    • 好的,我明白了,但是如果用户点击一次然后留下来,我将如何显示总数,它将是 a1+a2+a3 而不是 a4 但我不知道如何做一个这样的例外
    • @MacKenzieCutler 我知道,所以我现在正在编辑答案:)
    • @MacKenzieCutler 随时!
    【解决方案2】:

    你总是这样计算总数:

    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,并且仅在它们“命中”时分配值。

    另一种方法是将数字存储在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);
    

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

    【讨论】:

    • 感谢您的帮助!尽管我确信它可以工作但我没有将其标记为正确答案的唯一原因是因为我不太明白它为什么会工作。无论如何谢谢!
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-01-03
      • 2011-02-02
      • 1970-01-01
      • 2018-09-10
      • 2012-03-15
      • 1970-01-01
      • 2016-12-29
      • 2020-04-20
      • 2017-02-27
      相关资源
      最近更新 更多