Java - 嵌套While循环

Jef*_*eel 6 java while-loop switch-statement

Greetings Stack Overflow用户,今晚我来找你,帮助我创建一个Java程序.我对Java比较新,所以请原谅我对这个主题的无知.我制作了一个Java程序,它是一个"Rock""Paper""Scissors"游戏,其中一个语句中似乎有错误.

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;
            } 
        }
    }         
}
}
Run Code Online (Sandbox Code Playgroud)

我面临的问题与中奖计算有关.当我运行程序并且我反复进入摇滚直到我赢了,输出将是你赢了,"摇滚"击败""但是除了任何其他选项我得到你赢了,"摇滚"击败"PAPER"

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

*此外,如果你愿意指出任何其他建议,以帮助一个伟大的新手.*

Pau*_*per 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";
        }
Run Code Online (Sandbox Code Playgroud)

建议:

简明扼要.你可以改变

String startGame;
startGame = input.nextLine();
startGame = startGame.toUpperCase();
Run Code Online (Sandbox Code Playgroud)

String startGame = input.nextLine().toUpperCase();
Run Code Online (Sandbox Code Playgroud)

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

另外,知道equalsIgnoreCase()存在.

  • 不要忘记添加,如果你想要1到3之间的数字,你可以这样做:1 +(int)Math.random()*3; (2认同)