【问题标题】:Java: Ask user if they want to play again? [closed]Java:询问用户是否想再玩一次? [关闭]
【发布时间】:2016-09-22 07:18:14
【问题描述】:

我正在制作一个石头剪刀布游戏,一旦计算机或用户有一个最好的 2/3,那么它应该提示他们是否想再次玩游戏。如果“是”则程序重新启动,如果“否”则 system.exit 我猜。

我该怎么办?

    public static void main(String[] args) {
    Random r = new Random();
    int gameCount = 0;
    int computerWins = 0;
    int playerWins = 0;
    int rock, paper, scissors;
    rock = 1;
    paper = 2;
    scissors = 3;
    int playerChoice = 0;
    int computerChoice;

    System.out.println("Welcome to Rock Paper Scissors! Best 2 out of 3!");

    //While the game count is less than 3, the loop will repeat
    while (gameCount < 3) {
        computerChoice = r.nextInt(3) + 1;
        System.out.println("Enter \"Rock\", \"Paper\", or \"Scissors\"");
        String USER_Input = userInput.next();
        if (USER_Input.equalsIgnoreCase("Rock")) {
            playerChoice = 1;
        }
        if (USER_Input.equalsIgnoreCase("Paper")) {
            playerChoice = 2;
        }
        if (USER_Input.equalsIgnoreCase("Scissors")) {
            playerChoice = 3;
        }
        //If player enters anything besides rock, paper, or scissors
        if (playerChoice <= 0 || playerChoice > 3) {
            System.out.println("That wasn't an option");
            computerWins++;
            gameCount++;
            System.out.println("Not a valid input! Computer Wins!\n" +
                    "Player has won " +playerWins + " times and the computer " +
                    "has won " + computerWins + " times");

            //The game goes on, and the winners are added up!
        } else if (playerChoice == 1 && computerChoice == 2) {
            computerWins++;
            gameCount++;
            System.out.println("Rock v Paper! Computer Wins!\n" +
                    "Player has won " + playerWins + " times and the computer " +
                    "has won " + computerWins + " times");
        } else if (playerChoice == 2 && computerChoice == 1) {
            playerWins++;
            gameCount++;
            System.out.println("Paper v Rock! Player Wins!\n" +
                    "Player has won " + playerWins + " times and the computer " +
                    "has won " + computerWins + " times");
        } else if (playerChoice == 2 && computerChoice == 3) {
            computerWins++;
            gameCount++;
            System.out.println("Paper v Scissors! Computer Wins!\n" +
                    "Player has won " + playerWins + " times and the computer " +
                    "has won " + computerWins + " times");
        } else if (playerChoice == 3 && computerChoice == 2) {
            playerWins++;
            gameCount++;
            System.out.println("Scissors v Paper! Player Wins!\n" +
                    "Player has won " + playerWins + " times and the computer " +
                    "has won " + computerWins + " times");
        } else if (playerChoice == 3 && computerChoice == 1) {
            computerWins++;
            gameCount++;
            System.out.println("Scissors v Rock! Computer Wins!\n" +
                    "Player has won " + playerWins + " times and the computer " +
                    "has won " + computerWins + " times");
        } else if (playerChoice == 1 && computerChoice == 3) {
            playerWins++;
            gameCount++;
            System.out.println("Rock v Scissors! Player Wins!\n" +
                    "Player has won " + playerWins + " times and the computer " +
                    "has won " + computerWins + " times");
        } else if (playerChoice == 1 && computerChoice == 1) {
            gameCount++;
            System.out.println("Rock v Rock! Tie!\n" +
                    "Player has won " + playerWins + " times and the computer " +
                    "has won " + computerWins + " times");
        } else if (playerChoice == 2 && computerChoice == 2) {
            gameCount++;
            System.out.println("Paper v Paper! Tie!\n" +
                    "Player has won " + playerWins + " times and the computer " +
                    "has won " + computerWins + " times");
        } else if (playerChoice == 3 && computerChoice == 3) {
            gameCount++;
            System.out.println("Paper v Paper! Tie!\n" +
                    "Player has won " + playerWins + " times and the computer " +
                    "has won " + computerWins + " times");
        }

        //Check if game count reaches max games then chooses a winner
        if (gameCount == 3 && computerWins > playerWins || computerWins == 2) {
            System.out.println("The Computer Wins!");
            break;
        } else if (gameCount == 3 && computerWins < playerWins || playerWins ==2) {
            System.out.println("The Player Wins!");
        } else if (gameCount == 3 && computerWins == playerWins) {
            System.out.println("The game is a tie!");
        }
    }
}

【问题讨论】:

  • 检查用户输入,如果是“是”或其他内容,则调用方法 main()
  • "That wasn't an option" =&gt; computerWins++ 你得到我的 +1
  • 哈哈谢谢fr_andres ;) 电脑总是赢! @ivan 我不知道你可以再次调用 main 方法,这很有趣

标签: java


【解决方案1】:

您的整个逻辑都在 main() 方法中。我建议您将 while{} 部分移到 play() 之类的新方法中。

在您的 main() 中实现一个新的 while() 提示您的消息并从键盘读取输入:

public static void main(String args[]) {
        Scanner inputScanner = new Scanner(System.in);
        String userInput = "";
        do {
            play();
            System.out.print("Do you want to play again ([Y] / n)? ");
            userInput = inputScanner.nextLine();
            System.out.println(userInput);
        } while (userInput.equals("Y") || userInput.equals(""));
}

高温超导

【讨论】:

  • 很抱歉,我还没有在主要方法之外制作过另一种方法。如果我把它放在我的其他代码之前,它不会要求用户在发生任何其他事情之前再次播放以让你达到这一点吗?我只是想弄清楚如何在我的代码中实现它以获得精确的拟合
  • 嗨克里斯托弗。不,它不会因为 main() 方法是程序入口点。主要的第 1 行和第 2 行是“只是变量声明”。第 3 行是您请求的“再次播放”循环的开始。然后在带有“play()”的第 4 行中,您的新函数 private static void play() { your whole loop form your original post } 被执行。此时没有提示发生。第一个提示出现在第一次调用 play() 之后的第 5 行和第 6 行;
【解决方案2】:

一种可能的解决方案是将游戏放在另一个 while 循环中(包含 gameCount 的循环),并在 while 循环中为变量创建一个 switch 语句,比如说gameContinue。在游戏结束时,用户会收到一个提示,说明是否继续。根据他们的回答,您可以更改变量 gameContinue 的值,然后就可以了。

【讨论】:

    猜你喜欢
    • 2020-02-15
    • 2016-06-02
    • 2012-02-19
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多