Java - 嵌套While循环

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”直到获胜时,输出将为“您赢了,“ROCK”击败了“”,但选择任何其他选项时,我会得到“您赢了,“ROCK”击败了“PAPER”

我的问题是,为什么我在玩“rock”时会得到一个空回调?

*如果您愿意指出任何其他帮助初学者的建议,那就太好了。*

3个回答

5
Math.random() * 3 是一个大于等于0且小于3的数字。将其转换为整数后,它可以是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();

to

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

当您不必来回滚动页面时,内容更易于阅读。

此外,请注意存在 equalsIgnoreCase() 方法。


更好的做法是:如果(computerResult == 0){ playerComputer = "石头"; } else if(computerResult == 1){ playerComputer = "布"; } 否则{ playerComputer = "剪刀"; } - Ganesh Bhosle
2
不要忘记,如果你想要一个1到3之间的数字,可以这样做:1 + (int)Math.random() * 3; - Amir Afghani
很好的观点@AmirAfghani,尽管在这种情况下,0、1和2对大多数程序员来说是自然的。 - Paul Draper
你介意我再问一个快速问题吗?例如,如果玩家获胜,有没有一种方法进入if语句?我想在最后提供统计信息,我考虑添加类似于if(playerHumanWins = 1){<statement>}的内容。 - Jeffery Keel
我不认为我完全理解那个陈述。 - Jeffery Keel
是的...一个评论不足以回答另一个问题...这是我对你的程序的版本。希望它有意义。http://pastebin.com/xUA3xNYN - Paul Draper

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 (…) {
    …
  }
}

那样你就有一部分代码处理游戏本身,而另一部分代码处理用户交互。

0
你会发现 (int)(Math.random() * 3) 有时会得到0,但永远不会得到3,这就是为什么你的结果为空,因为你没有针对0的结果。
具体来说,当 Math.random() 返回小于 .33 时会出现这种情况。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接