0

我正在开发一个 Java 程序,我正在努力为我的程序创建我称之为统计区域的东西。我创建了一个“摇滚”“纸”“剪刀”程序,我试图告诉用户他们赢了多少次,计算机赢了多少次,最后,有多少平局。

请原谅我的无知,我正在学习。这是我的代码:

 import java.util.Scanner;

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

        int playerHumanWins = 0;
        int playerComputerWins = 0;
        int numberOfTies = 0;
        int computerResult;

        Scanner input = new Scanner(System.in);

        while(true) {

            String startGame;
            String playerHuman;
            String playerComputer = " ";

            System.out.print("Do you want to play \"Rock\", \"Paper\", \"Scissors\"? (Y/N): ");
            startGame = input.nextLine().toUpperCase();

            if(startGame.equals("N")) {
                System.out.println("NO!");
                break;
            }
            else if(! startGame.equals("Y")) {
                startGame = startGame.toLowerCase();
                System.out.println("Sorry, " + startGame + " is not a valid entry...");               
            }
            while(startGame.equals("Y")) {
                System.out.print("Please choose \"Rock\", \"Paper\", or \"Scissors\": ");
                playerHuman = input.nextLine().toUpperCase();

                computerResult = (int)(Math.random() * 3);

                if(computerResult == 0) {
                    playerComputer = "ROCK";
                }
                else if(computerResult == 1) {
                    playerComputer = "PAPER";
                }
                else if (computerResult == 2) {
                    playerComputer = "SCISSORS";
                }

                switch (playerHuman) {
                    case "ROCK" :
                        if(playerComputer.equals(playerHuman)) {
                        System.out.println("Tie you both picked \"ROCK\"");
                        numberOfTies++;
                    }
                        else if(playerComputer.equals("PAPER")) {
                            System.out.println("Sorry, the computer wins!");
                            playerComputerWins++;
                        }
                        else {
                            System.out.println("You win, \"ROCK\" beats " + "\"" + playerComputer + "\"");
                            playerHumanWins++;
                            return;
                        }
                        break;
                    case "PAPER" :
                        if(playerComputer.equals(playerHuman)) {
                        System.out.println("Tie you both picked \"PAPER\"");
                        numberOfTies++;
                    }
                        else if(playerComputer.equals("ROCK")) {
                            System.out.println("You win, \"PAPER\" beats " + "\"" + playerComputer + "\"");
                            playerHumanWins++;
                            return;
                        }
                        else {
                            System.out.println("Sorry, the computer won!");
                            playerComputerWins++;
                        }
                        break;
                    case "SCISSORS" :
                        if(playerComputer.equals(playerHuman)) {
                        System.out.println("Tie you both picked \"SCISSORS\"");
                        numberOfTies++;
                    }
                        else if(playerComputer.equals("PAPER")) {
                            System.out.println("You win, \"SCISSORS\" beats " + "\"" + playerComputer + "\"");
                            playerHumanWins++;
                            return;
                        }
                        else {
                            System.out.println("Sorry, the computer won!");
                            playerComputerWins++;
                        }
                        break;
                    default:
                        playerHuman = playerHuman.toLowerCase();
                        System.out.println("Sorry, " + playerHuman + " is not a valid entry...");      
                }

                if(playerHumanWins == 1) {
                    System.out.println("You won: " + playerHumanWins + " times");
                    System.out.println("The computer won " + playerComputerWins + " times");
                    System.out.println("There were " + numberOfTies++);
                }
            }
        }
    }
}

基本上我已经做了一个 if 语句,说当用户至少赢一次时,显示统计信息但是这似乎不起作用。我不能 100% 确定如何显示统计信息。

4

3 回答 3

2

基本上,return您的代码中的语句并强制 Java 退出您的 ( main) 方法,这会阻止它执行代码的“统计”部分并终止您的程序。

首先删除它们。

还...

if (playerHumanWins == 1) {...

意味着统计数据只会显示一次,当玩家第一次获胜时。不确定这是否真的想要你想要的......

更新

基于游戏只能在玩家获胜之前结束的条件,您可以执行类似...

    if (playerHumanWins == 1) {
        break;
    }
}

System.out.println("You won: " + playerHumanWins + " times");
System.out.println("The computer won " + playerComputerWins + " times");
System.out.println("There were " + numberOfTies++);

或者...

while (startGame.equalsIgnoreCase("Y") && playerHumanWins < 1) {
    //...
}

System.out.println("You won: " + playerHumanWins + " times");
System.out.println("The computer won " + playerComputerWins + " times");
System.out.println("There were " + numberOfTies++);

无论哪种方式,你也应该摆脱这种while (true) {说法,它增加了不必要的复杂性......

于 2013-10-31T05:53:50.447 回答
0

您正在使用return跳出开关。Return 退出该方法。不如break改用!

还要确保在玩新游戏之前将 playerHumanWins 和其他人的值重置为零

于 2013-10-31T05:54:22.117 回答
0

修改一些改动如下:

 else if(! startGame.equals("Y")) {
     startGame = startGame.**toLowerCase()**;
     System.out.println("Sorry, " + startGame + " is not a valid entry...");               
 }

else if(! startGame.equals("Y")) {
    startGame = startGame.**toUpperCase()**;
    System.out.println("Sorry, " + startGame + " is not a valid entry...");               
}
于 2013-10-31T05:55:57.533 回答