二十一点程序即使他们说“留下”也总是强迫用户“击中”

时间:2018-04-03 16:28:17

标签: java blackjack

所以我正在制作二十一点计划。除了一个bug类型之外,我已经成功完成了游戏。用户可以像往常一样在二十一点中获得“击中”或“停留”的选项,但是当它最终告诉他们他们的总数时,即使他们说保持两次也会增加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.");
    }
  }
}

我只是想知道你是否认为我做错了什么或者应该做些什么来使它正常工作。

3 个答案:

答案 0 :(得分:0)

问题非常简单但难以捉摸。

您已为第一次显示a1+a2。但是,在显示结果时,您会显示a1+a2+a3+a4。现在,a3和a4已经初始化为一些随机数,因此最终结果总是会超过最初的结果,即使你留下来也是如此。两次。

解决方案 -

  1. 要解决此问题,您可以创建名为int UserTotal = a1+a2的变量,并在用户点击“{1}}时添加a3”。第一次,如果用户点击“a4,则totala第二次。然后,将a3初始化为UserInput。
  2. 或者,您可以在计划开始时将a40设置为a3,如果用户“点击”{第一次,您可以将a4设置为随机值,如果用户第二次点击,Pattern wDecimal = Pattern.compile("([\\d]+[.][\\d+][\'])"); // 12.5' 可以设置为随机值。

答案 1 :(得分:0)

你总是这样计算总数:

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);

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

答案 2 :(得分:0)

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

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