为什么我的游戏代码没有根据用户输入循环返回和退出?

Why is my game code not looping back and exiting based on user input?

我正在尝试用 drjava 制作一个游戏,其中两名玩家玩名为 'Remove the Last Stone' 的双人游戏。我已经成功编写了游戏的核心代码。但是,在游戏结束后,我需要为用户提供三个选项,说明他们在玩初始游戏后希望如何继续。三个选项是:

1) 与相同的玩家 玩另一场游戏。 2) 退出游戏。 3) 与 不同的玩家.

玩另一场游戏

所以基本上,我的游戏会提示最初开始玩游戏的两个玩家如果想玩另一个游戏则输入“1”。按下这个键的作用,不是再次询问玩家的名字,而是直接询问他们想玩多少轮,然后继续剩下的游戏。在初始游戏结束时,如果有 2 other/different 位玩家想玩游戏,则用户可以输入“2”。在这种情况下,我的代码应该做的是循环回到代码的最开头,其中它询问玩家的名字,因为有两个 new 玩家正在玩.最后,用户可以选择输入“3”退出游戏。我曾尝试对此进行编码,但出于某种原因,我遇到了基于用户在按下“1”和“2”之间的输入而循环回游戏代码的 2 个不同部分的问题。当用户选择按 3 时,我在退出游戏时也遇到了问题。我的问题是,当按下这 3 个键中的任何一个(“1”、“2”或“3”)时,我的游戏会反复询问如何用户想玩的多轮。这不应该发生,因为这 3 个键应该执行 不同的 操作。

这是我的游戏代码:

// Import the Scanner class
import java.util.Scanner;

public class StrategyGame {
    public static void main(String[] args) {

        // Create a new Scanner object for keyboard input
        Scanner keyboard = new Scanner(System.in);

        // Variable declarations
        String namePlayerOne, namePlayerTwo;
        String lastPlayerTurn = "";
        boolean playDifferentGame = true;
        boolean playSameGame = true;
        boolean playRounds = true;
        boolean validInputPlayerOne = false;
        boolean validInputPlayerTwo = false;
        boolean validProceed = false;
        int userGameRounds, playerRemoveLimit, gameStonePile, playerTwoRemoval, proceedChoice;
        int playerOneRemoval = 0;
        int numRounds = 0;
        int playerOneWins = 0;
        int playerTwoWins = 0;

        // Do while loop responsible for executing whole game first, then repeatedly loops if the same two 
        // players that started the game want to play again
        do {
            System.out.println();
            System.out.println("Player 1, please enter your name:");
            namePlayerOne = keyboard.nextLine();
            System.out.println("Player 2, please enter your name:");
            namePlayerTwo = keyboard.nextLine();
            System.out.println();

            // Display game instructions and how to play game
            System.out.println(namePlayerOne + " and " + namePlayerTwo + ", welcome to the Strategy Game!"
                                   + " The rules of this game are very simple. You guys will be iteratively asked"
                                   + " to remove some stones from a pile of stones, which will total different amounts"
                                   + " for each round. For each round, you will only be allowed to remove a specific maximum"
                                   + " number of stones. A round from the game will finish when either one of you removes the"
                                   + " remaining stones and there are zero stones left in the pile. You will be asked how many"
                                   + " rounds you want to play, and so, a game will finish when your chosen number of rounds"
                                   + " have been completed.");

            // Do while loop that repeatedly loops, after the very first game, if two different players want to play
            do {
                System.out.println();
                System.out.println("How many rounds would you like to play? You can select between 2-5 rounds inclusively.");
                userGameRounds = keyboard.nextInt();

                // Ask user how many rounds they want to play, between 2-5 rounds (inclusively)
                while (userGameRounds < 2 || userGameRounds > 5) {
                    System.out.println();
                    System.out.println("Invalid input. Please try again.");
                    userGameRounds = keyboard.nextInt();
                }

                // If user-input is valid; enters into execution of game rounds
                while (playRounds == true) {
                    gameStonePile = (int) (11 * Math.random() + 10);
                    playerRemoveLimit = (int) (3 * Math.random() + 2);
                    numRounds = numRounds + 1;
                    System.out.println();
                    System.out.println("It is now round " + numRounds + ". For each turn, each player"
                                           + " is only allowed to remove a maximum of " + playerRemoveLimit + " stones.");
                    System.out.println("** Important Note: Each player should also avoid removing stones that exceed"
                                           + " the number of stones that are remianing in the stone pile, even if the amount you"
                                           + " remove is within the removing limit range. (i.e., You remove 3 stones, when 2 stones are left in pile.)" 
                                           + " **");

                    // Until game stone pile reaches zero, the 2 players continue alternating turns
                    while (gameStonePile != 0) {
                        if (gameStonePile != 0) {
                            do {
                                validInputPlayerOne = false;
                                System.out.println();

                                // Player One's turn
                                System.out.println(namePlayerOne + ", it is your turn. Please enter the amount of stones you want to remove."
                                                       + " In the stone pile currently, there are " + gameStonePile + " stone(s).");

                                // Get amount Player 1 wants to remove from current/remaining stone pile
                                playerOneRemoval = keyboard.nextInt();
                                while (playerOneRemoval < 1 || playerOneRemoval > playerRemoveLimit || playerOneRemoval > gameStonePile) {
                                    System.out.println("Invalid input. Please try again.");
                                    playerOneRemoval = keyboard.nextInt();
                                }

                                // Subtract amount Player 1 decides to remove from the remaining game stone pile
                                validInputPlayerOne = true;
                                gameStonePile = gameStonePile - playerOneRemoval;

                                // Record that Player 1 was the most recent player that removed stones from pile
                                lastPlayerTurn = namePlayerOne;

                                // When the stone pile reaches zero, determine Player 1 as winner 
                                if (gameStonePile == 0) {
                                    playerOneWins = playerOneWins + 1;
                                    System.out.println();
                                    System.out.println(namePlayerOne + " wins round #" + numRounds + "! The current score is:");
                                    System.out.println();
                                    System.out.println(namePlayerOne + ": " + playerOneWins);
                                    System.out.println(namePlayerTwo + ": " + playerTwoWins);
                                }

                            } while (playerOneRemoval >= 1 && playerOneRemoval <= playerRemoveLimit && validInputPlayerOne == false);
                        }
                        if (gameStonePile != 0) {
                            do {
                                validInputPlayerTwo = false;

                                // Player Two's turn
                                System.out.println(namePlayerTwo + ", it is your turn. Please enter the amount of stones you want to"
                                                       + " remove. In the stone pile currently, there are " + gameStonePile + " stone(s).");

                                // Get amount Player 2 wants to remove from current/remaining stone pile
                                playerTwoRemoval = keyboard.nextInt();
                                while (playerTwoRemoval < 1 || playerTwoRemoval > playerRemoveLimit || playerTwoRemoval > gameStonePile) {
                                    System.out.println("Invalid input. Please try again.");
                                    playerTwoRemoval = keyboard.nextInt();
                                }
                                validInputPlayerTwo = true;

                                // Subtract amount Player 2 decides to remove from the remaining game stone pile
                                gameStonePile = gameStonePile - playerTwoRemoval;

                                // Record that Player 1 was the most recent player that removed stones from pile
                                lastPlayerTurn = namePlayerTwo;
                                if (gameStonePile == 0) {
                                    playerTwoWins = playerTwoWins + 1;
                                    System.out.println();
                                    System.out.println(namePlayerTwo + " wins round #" + numRounds + "! The current score is:");
                                    System.out.println();
                                    System.out.println(namePlayerOne + ": " + playerOneWins);
                                    System.out.println(namePlayerTwo + ": " + playerTwoWins);
                                }
                            } while (playerTwoRemoval >= 1 && playerOneRemoval <= playerRemoveLimit && validInputPlayerTwo == false);
                        }

                        // When the user-requested # of rounds have been reached and game-stone pile reaches zero from
                        // very last round, display end of game message
                        if (numRounds == userGameRounds && gameStonePile == 0) {
                            numRounds = 0;
                            playRounds = false;
                            System.out.println();
                            System.out.println("Hooray! The game has finished!");

                            // Determine the best player so far and their # of wins
                            if (playerOneWins > playerTwoWins) {
                                System.out.print(namePlayerOne + " is the best player so far with " + playerOneWins + " win(s).");
                            }
                            else if (playerTwoWins > playerOneWins) {
                                System.out.print(namePlayerTwo + " is the best player so far with " + playerTwoWins + " win(s).");
                            }
                            else{
                                System.out.print(namePlayerOne + " and " + namePlayerTwo + ", it looks like it's a draw!");
                            }
                        }

                        // Once game between two players  finishes, ask user how they want to proceed based on three 
                        // options
                        if (numRounds == 0){
                            System.out.println();
                            System.out.println("How would you like to proceed? You have three options. Enter '1' if you would like to"
                                                   + " play another game with the same players. Enter '2' if you want to exit the game."
                                                   + " Enter '3' to play another game with different players.");

                            // Get user-input of how they want to proceed
                            proceedChoice = keyboard.nextInt();

                            // Check for valid input
                            while (proceedChoice <1 || proceedChoice >3){
                                validProceed = false;
                                System.out.println("Invalid input. Please try again.");
                                proceedChoice = keyboard.nextInt();
                            }
                            validProceed = true;
                            // Loop back accordingly to if the same players want to play a new game, different players 
                            // want to play a new game, and exit the game if user chooses to do so
                            if (proceedChoice == 1) {
                                playSameGame = true;
                            }
                            else if (proceedChoice == 2) {
                                playDifferentGame = true;
                            }
                            else if (proceedChoice == 3){
                                playDifferentGame = false;
                            }
                        }
                    }
                }
            } while (playSameGame == true);
        } while (playDifferentGame == true);

        keyboard.close();

    } // main method
} // Strategy Game class

我知道这个问题与我的循环有关,但我似乎无法弄清楚。我会很感激一些帮助。谢谢!

您的循环查找您的 playSameGame 变量是否为 false 以退出。但它永远不会在您的代码中的任何地方设置为 false。所以它会永远重复。

我同意 IanGabes 的评论,因为他写了很好的堆栈溢出问题,但也只是为了学习如何自己调试这些东西。最好的方法是当你遇到问题时让你的代码尽可能简单。