6

问候 Stack Overflow 用户,今天晚上我来找你寻求关于我创建的 Java 程序的帮助。我对 Java 比较陌生,所以请原谅我对这个话题的无知。我制作了一个 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();

        startGame = startGame.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();

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

            playerHuman = playerHuman.toUpperCase();

            if(computerResult == 1) {
                playerComputer = "ROCK";
            }
            else if(computerResult == 2) {
                playerComputer = "PAPER";
            }
            else if (computerResult == 3) {
                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("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..."); 
                    break;
            } 
        }
    }         
}
}

我面临的问题与获胜计算有关。当我运行程序并重复输入 rock 直到我获胜时,输出将是You win, "ROCK" beats " "但如果有任何其他选项我会得到You win, "ROCK" beats "PAPER"

我的问题是,为什么我在玩摇滚时得到一个空的回调?

*此外,如果您愿意指出任何其他建议来帮助新手,那就太好了。*

4

3 回答 3

5

Math.random() * 3是一个至少为 0 且小于 3 的数。

将其转换为 int 后,它是 0、1 或 2。

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

建议:

简明扼要。你可以改变

String startGame;
startGame = input.nextLine();
startGame = startGame.toUpperCase();

String startGame = input.nextLine().toUpperCase();

当您不必滚动和滚动时,它更具可读性。

另外,要知道equalsIgnoreCase()存在。

于 2013-10-31T04:48:32.267 回答
1

这不适合完全的新手,但我会使用以下代码对游戏进行建模:

enum Choice { ROCK, PAPER, SCISSORS }

enum Result { COMPUTER_WINS, TIE, HUMAN_WINS }

Result decide(Choice computer, Choice human) {
  if (human == computer) {
    return Result.TIE;
  } else if (…) {
    …
  }
}

这样一来,您就拥有了一些处理游戏本身的代码,而其他一些代码则处理了用户交互。

于 2013-10-31T05:02:18.830 回答
0

您会发现 (int)(Math.random() * 3) 有时会产生 0 而从不会产生 3,这就是给您空白的原因,因为您没有 0 的结果。

具体来说,当 Math.random() 返回小于 0.33 时

于 2013-10-31T04:51:21.497 回答