Java 简单的21点游戏;走错路:需要帮助

Java 简单的21点游戏;走错路:需要帮助,java,Java,该程序的目的是创建一个随机数生成器,并选择编号为1-11的卡片(11为Ace)。游戏规则没有完成的工作程序那么重要,我不需要西装、钱或高分表 我试图创建一个卡片生成器,每次调用它时都会生成一个随机的卡片值,但失败了。正如您将在代码中看到的,int card1=1+r.nextInt();每次返回相同的值。如果card1调用4,则所有未来的card1也将是4。这不是我在创建int-value 1-11 inc.时的想法。我希望card1每次都会返回一个随机值 接下来,我尝试将卡总数和新卡1添加到当

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

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

接下来,我尝试将卡总数和新卡1添加到当前整数,但失败。这也无法按预期工作

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

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

            }
      }


  }

  }

正如你所看到的,我还没有谈到经销商的部分

在while循环中再次调用random:

int newCard = 1 + r.nextInt(11);
这会将变量设置为新的随机值(如果需要重置原始卡变量,也可以这样做)。然后确保你检查了用户的输入与这个新的随机卡


在这项工作完成后,您可能需要查看一些有关构建程序的提示。

问题在于,您在初始化卡时应用了随机化。以这种方式申报您的卡:

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

使用您当前的方法,这将帮助您启动新一轮

    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和random card的评论,你应该走得很好。

这应该会有帮助。这是一个多么好的主意,你在这个论坛上的讽刺有没有得分?你指的是什么方法?那么你建议我如何运用这些方法来创造,我已经失败了吗?@hs2345我会看一看,谢谢。@Vincent我喜欢你在文章的最后一行批评讽刺。我相信他只想在循环中生成下一张卡,但你明白了……这正是我想做的,唯一的问题是我不知道那应该是什么样子。你能给我一个有限的工作例子吗?我真的不知道你现在还想看什么。你不明白如何应用一种方法吗?编辑:添加了示例。如果我再这样做,恐怕你什么也学不到。我不知道如何运用我还没有学会的方法,遗憾的是,我是一个完全的初学者,我们所做的“学校作业”根本没有达到这一点。编辑:谢谢你的例子,只要我理解了这个方法背后的逻辑,我就会学习它。没有什么比一个小哈维更能让人觉得自己是个白痴:)在你的修订版中有很多我不理解的地方。您声明int值的方式,每个相关类型=0,我假设这是为了确保每个新回合都有干净的记录。newRound=false,在代码中何时声明为true?或者这应该是真的吗?我开始觉得我的java讲师是个白痴,不能教这门课。我在这些论坛上学到的东西比我在大学前6个月学到的要多。谢谢你的帮助,我不会让你失望的热情。当您想在
while
中进行下一个循环时,您应该设置
newRound=true
,以进行新的交易。在你的情况下,这意味着给所有的牌分配新的值。因此,如果我在代码中添加一个你想要一个新的游戏行,我可以跳回新回合并重新开始?当然!那是个好主意。在循环结束时使用
扫描仪
,检查是否应设置
newRound=true
,或完全退出循环。我希望有人仍在关注此线程!我现在已经排除了大部分错误,我只是无法使随机卡生成器正常工作。每次我尝试使用int类时,它都会停止工作,我在许多地方都尝试过使用它,但仍然无法编译。