Java骰子游戏无法正确进行后续游戏:布尔变量问题?

时间:2019-02-28 17:53:25

标签: java

我一直在从事骰子游戏,最后使它起作用(通过这种方式,我是说我已经生成了.class文件)。但是,我无法弄清楚游戏逻辑存在问题。我认为该程序错误地解释了游戏规则。

以下是规则:玩家掷四个骰子一轮。在第一轮中 如果掷骰子的总和等于6、12、13、17、19或23,则玩家获胜,游戏结束。如果玩家在本回合中掷出9、11、18或24,则玩家输。如果玩家掷出任何其他数字,则骰子的总和将成为“目标数”,游戏将继续掷出骰子。 玩家必须继续掷骰子,直到掷出23并输掉为止,或者再次从第一轮掷出骰子的总和,然后获胜。

运行游戏时,它会从第一轮开始一直使用规则,尽管规则在随后的几轮中都会更改。

例如,这是我从控制台获得的输出之一

  

您的骰子值总和:15。您掷出15,所以游戏将一直玩直到再次掷出该骰子。(然后第二轮掷出)骰子值:17.您赢了!

显然这是不对的,因为在这种情况下,只有再次输15才能赢。显然,它仍然是第一次使用获胜值,我不明白,因为在条件keepPlaying = true和justOneRound = false的情况下,我有第二轮的do-while循环,但仍然没有意识到justOneRound是false接下来的回合。

这是代码,(我知道这听起来很混乱,但希望您一看便知道了):

// Declare boolean variables to tell if the user keeps playing in subsequent rounds (to be used by multiple methods).
public static boolean justOneRound = false;
public static boolean keepPlaying = true;

// Declare variables for the goal number and the sum of the dice on the first round. 
public static int firstRoundDiceSum = 0;

// main method header
    public static void main (String [] args)
    {
        // Play the game for at least one round, and then again if the user doesn't win or lose.
            do 
            {
                // Roll the dice (redirect to the roll() method) and declare a variable to hold the sum of the values that have returned. 
                firstRoundDiceSum = roll();

                // Print the sum of the rolled dice. 
                System.out.println("Your sum of the dice values: ");
                System.out.println(firstRoundDiceSum);

                // Determine if the user won or not. 
                if (firstRoundDiceSum == 6 ||firstRoundDiceSum == 12 ||firstRoundDiceSum == 13 ||firstRoundDiceSum == 17 || firstRoundDiceSum == 19 ||firstRoundDiceSum == 23)
                {
                    justOneRound = true;
                    keepPlaying = false;
                    System.out.println("You win!");
                    System.exit(0);
                }
                else if (firstRoundDiceSum == 9 ||firstRoundDiceSum == 11 ||firstRoundDiceSum == 18 ||firstRoundDiceSum == 24)
                {
                    justOneRound = true;
                    keepPlaying = false;
                    System.out.println("You lose!");
                    System.exit(0);
                }
                else 
                {
                    justOneRound = false;
                    keepPlaying = true;
                    System.out.println("You rolled a sum of: " + firstRoundDiceSum + ", so the game will keep playing until you roll this sum again.");
                }
            } while (justOneRound = true);

        // Play the game for subsequent rounds until the user gets the goal number or loses.
        do 
            {   
                // Roll the dice (redirect to the roll2() method) and declare a variable to hold the sum of the values that have returned. 
                int laterRoundsDiceSum = roll2();

                // Print the sum of the rolled dice. 
                System.out.println("Your sum of the dice values: ");
                System.out.println(laterRoundsDiceSum);

                // Determine if the user won or not. 
                if (laterRoundsDiceSum == firstRoundDiceSum)
                {
                    keepPlaying = false;
                    System.out.println("You win!");
                    System.exit(0);
                }
                else if (laterRoundsDiceSum == 23)
                {
                    keepPlaying = false;
                    System.out.println("You lose!");
                    System.exit(0);
                }
                else 
                {
                    keepPlaying = true;
                    System.out.println("You didn't roll " + firstRoundDiceSum + ", so you have to keep rolling until you get it!");
                }
            } while ((justOneRound = false) && (keepPlaying = true));
    }

    // roll() method header that rolls the die for the first round (creates a dieRoll object and gets a random value for the die).
    public static int roll()
    {
        do 
        {
            // Create a random class object.
            Random dieRoll = new Random();

            // Declare a variable for the sum, and set it to zero before the dice roll.
            int firstRoundDiceSum = 0;

            // Use a for-loop to roll the dice four times (with counter variable timesRolled).

            for (int timesRolled = 1; timesRolled <= 5; timesRolled ++)
            {
                int dieValue = dieRoll.nextInt(6) + 1;
                firstRoundDiceSum = firstRoundDiceSum + dieValue;
            }
            return firstRoundDiceSum;
        } while (justOneRound = true);
    }

    // roll() method header that rolls the die for the subsequent round(s) (creates a dieRoll object and gets a random value for the die).
    public static int roll2()
    {
        do 
        {
            // Create a random class object.
            Random dieRoll = new Random();

            // Declare a variable for the sum, and set it to zero before the dice roll.
            int laterRoundsDiceSum = 0;

            // Use a for-loop to roll the dice four times (with counter variable timesRolled).

            for (int timesRolled = 1; timesRolled <= 5; timesRolled ++)
            {
                int dieValue = dieRoll.nextInt(6) + 1;
                laterRoundsDiceSum = laterRoundsDiceSum + dieValue;
            }
            return laterRoundsDiceSum;
        } while (justOneRound = true);
    }
}

0 个答案:

没有答案