简单的二十一点游戏;出错了:需要帮助

时间:2015-03-15 22:35:08

标签: java

该程序的目的是创建一个随机数生成器,并选择编号为1-11的卡(11为Ace)。游戏规则并不像完成的工作程序那么重要,我不需要西装,钱或高分表。

我尝试过创建一个卡片生成器,但每次调用它时都会生成一个随机卡片值。正如您将在代码中看到的,int card1 = 1 + r.nextInt();每次都返回相同的值。如果它为card1调用4,则所有未来的card1(s)也将是4.这不是我在创建int值1-11 inc时的想法。我曾希望card1每次都会返回一个随机值。

接下来我尝试了将card total和newCard1添加到当前int的尝试,但没有成功。这也无法按预期工作。

以下是我到目前为止尝试的代码,请记住,当您向我解释您的更正时,我是一年级学生,并且几乎没有基本知识:

  import java.util.Random;
  import java.util.Scanner;

  class blackj
{
public static void main(String[] args)
{
    Random r = new Random();
    Scanner keyboard = new Scanner(System.in);
    String name;

    boolean playing = true;
    boolean notPlaying = true;
    int card1 = 1 + r.nextInt(11);
    int card2 = 1 + r.nextInt(11);
    int dcard1 = 1 + r.nextInt(11);
    int dcard2 = 1 + r.nextInt(11);

    int ptotal = card1+card2;
    int dtotal = dcard1+dcard2;

    int pcurrent = ptotal+card1;
    int dcurrent =dtotal+dcard1;
    {   


        System.out.println("Welcome to Blackjack ! " );
            System.out.println("Score as close to 21 without going over to win ");
                System.out.println("What is your name?");
            name = keyboard.nextLine();
                System.out.println("Hello " + name);
                System.out.println("Let's play some BlackJack!");
            System.out.println("The dealer shows: \n\t\t" +dcard1 );
            System.out.println("Your first card is: \n\t\t " +card1 );
            System.out.println("Your second card is: \n\t\t" +card2  );
            System.out.println("Giving you a grand total of: " +ptotal );

        while (playing)
            {
            System.out.println("Would you like to (H)it or (S)tick?");
                Scanner hit1 = new Scanner(System.in);
                String a = hit1.nextLine();
                if(a.equals("s"))
            {
                System.out.println("You stick at " );
                System.out.println("Now it's the dealers turn\n Dealer must draw until 17");
            }
                if(a.equals("h"))
            {
                System.out.println("Your next card is " +card2 );
                System.out.println("Giving you a new total of "+pcurrent );
                    if ((pcurrent >=22))
                System.out.println("You Busted! \nSorry! you lose");
            }
            else
            {
                System.out.println("Please press H or S");
            }

            }
      }


  }

  }

正如你所看到的那样,我还没到经销商那里。

3 个答案:

答案 0 :(得分:2)

在你的while循环中再次调用random:

int newCard = 1 + r.nextInt(11);

这会将变量设置为新的随机值(如果您需要重置原始卡变量,也可以这样做)。然后确保根据这个新的随机卡检查用户的输入。

在您开始工作之后,您可能需要查看code review以获取有关构建程序的一些提示。

答案 1 :(得分:0)

问题是您在初始化卡片时应用了随机化。以这种方式宣布你的卡片:

int card1 = 1 + r.nextInt(11);
int card2 = 1 + r.nextInt(11);
int dcard1 = 1 + r.nextInt(11);
int dcard2 = 1 + r.nextInt(11);

在方法中进行随机化,如下所示:

public int getRandomInt()
{
    return 1 + r.nextInt(11);
}

并为您的卡分配值:

card1 = getRandomInt();

现在,无论何时为卡片分配值(即使是重复的卡片),您也会获得随机值。

System.out.println("Your next card is " +card2 );

可以替换为

card2 = getRandomInt();
System.out.println("Your next card is " +card2 );

答案 2 :(得分:0)

使用您当前的方法,这将有助于您开始新一轮

    Random r = new Random();
    Scanner keyboard = new Scanner(System.in);
    String name;

    boolean playing = true;

    System.out.println("Welcome to Blackjack ! ");
    System.out.println("Score as close to 21 without going over to win ");
    System.out.println("What is your name?");
    name = keyboard.nextLine();
    System.out.println("Hello " + name);
    int card1, card2 = 0, dcard1, dcard2, ptotal, dtotal, pcurrent = 0, dcurrent;
    boolean newRound = true;
    while (playing) {
        if (newRound) {
            newRound = false;
            card1 = 1 + r.nextInt(11);
            card2 = 1 + r.nextInt(11);
            dcard1 = 1 + r.nextInt(11);
            dcard2 = 1 + r.nextInt(11);

            ptotal = card1 + card2;
            dtotal = dcard1 + dcard2;

            pcurrent = ptotal + card1;
            dcurrent = dtotal + dcard1;
            System.out.println("Let's play some BlackJack!");
            System.out.println("The dealer shows: \n\t\t" + dcard1);
            System.out.println("Your first card is: \n\t\t " + card1);
            System.out.println("Your second card is: \n\t\t" + card2);
            System.out.println("Giving you a grand total of: " + ptotal);
        }

        System.out.println("Would you like to (H)it or (S)tick?");
        Scanner hit1 = new Scanner(System.in);
        String a = hit1.nextLine();
        if (a.equals("s")) {
            System.out.println("You stick at ");
            System.out.println("Now it's the dealers turn\n Dealer must draw until 17");
        }
        if (a.equals("h")) {
            System.out.println("Your next card is " + card2);
            System.out.println("Giving you a new total of " + pcurrent);
            if ((pcurrent >= 22)) {
                System.out.println("You Busted! \nSorry! you lose");
                newRound = true;
            }
        } else {
            System.out.println("Please press H or S");
        }

    }

将此与其他人所说的关于card2和随机卡片的内容相结合,你应该顺利完成。