Java:询问用户是否想再次玩?

Java:询问用户是否想再次玩?,java,Java,我正在做一个石头剪刀游戏,一旦电脑或用户有了一个最好的2/3,它就会提示他们是否想再次玩这个游戏。如果“是”,则程序重新启动,如果“否”,则系统退出 我该怎么做 public static void main(String[] args) { Random r = new Random(); int gameCount = 0; int computerWins = 0; int playerWins = 0; int rock, paper, s

我正在做一个石头剪刀游戏,一旦电脑或用户有了一个最好的2/3,它就会提示他们是否想再次玩这个游戏。如果“是”,则程序重新启动,如果“否”,则系统退出

我该怎么做

    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!");
        }
    }
}
publicstaticvoidmain(字符串[]args){
随机r=新随机();
int gameCount=0;
int=0;
int playerWins=0;
石头、布、剪刀;
岩石=1;
纸张=2;
剪刀=3;
int playerChoice=0;
国际计算机选择;
System.out.println(“欢迎来到石头剪刀!三分之二最好!”);
//当游戏计数小于3时,循环将重复
而(游戏数<3){
computerChoice=r.nextInt(3)+1;
System.out.println(“输入“岩石”、“纸”或“剪刀”);
字符串USER_Input=userInput.next();
if(用户输入。等效信号情况(“岩石”)){
playerChoice=1;
}
if(用户输入。相等信号情况(“纸张”)){
playerChoice=2;
}
if(用户输入。等效信号情况(“剪刀”)){
playerChoice=3;
}
//如果玩家输入除石头、布或剪刀以外的任何东西
如果(玩家选择3){
System.out.println(“这不是一个选项”);
computerWins++;
gameCount++;
System.out.println(“不是有效的输入!计算机获胜!\n”+
“玩家赢得了”+玩家+“时代和电脑”+
“已赢得”+计算机赢得”+“时代”);
//游戏继续进行,赢家加起来了!
}else if(playerChoice==1&&computerChoice==2){
computerWins++;
gameCount++;
System.out.println(“岩石对纸张!计算机获胜!\n”+
“玩家赢得了”+玩家+“时代和电脑”+
“已赢得”+计算机赢得”+“时代”);
}else if(playerChoice==2&&computerChoice==1){
playerWins++;
gameCount++;
System.out.println(“纸对石!玩家赢!\n”+
“玩家赢得了”+玩家+“时代和电脑”+
“已赢得”+计算机赢得”+“时代”);
}else if(playerChoice==2&&computerChoice==3){
computerWins++;
gameCount++;
System.out.println(“纸对剪刀!计算机赢!\n”+
“玩家赢得了”+玩家+“时代和电脑”+
“已赢得”+计算机赢得”+“时代”);
}else if(playerChoice==3&&computerChoice==2){
playerWins++;
gameCount++;
System.out.println(“剪刀对布!玩家赢!\n”+
“玩家赢得了”+玩家+“时代和电脑”+
“已赢得”+计算机赢得”+“时代”);
}else if(playerChoice==3&&computerChoice==1){
computerWins++;
gameCount++;
System.out.println(“剪刀对石头!电脑赢!\n”+
“玩家赢得了”+玩家+“时代和电脑”+
“已赢得”+计算机赢得”+“时代”);
}else if(playerChoice==1&&computerChoice==3){
playerWins++;
gameCount++;
System.out.println(“石头对剪刀!玩家赢!\n”+
“玩家赢得了”+玩家+“时代和电脑”+
“已赢得”+计算机赢得”+“时代”);
}else if(playerChoice==1&&computerChoice==1){
gameCount++;
System.out.println(“岩石对岩石!领带!\n”+
“玩家赢得了”+玩家+“时代和电脑”+
“已赢得”+计算机赢得”+“时代”);
}else if(playerChoice==2&&computerChoice==2){
gameCount++;
System.out.println(“纸张对纸张!领带!\n”+
“玩家赢得了”+玩家+“时代和电脑”+
“已赢得”+计算机赢得”+“时代”);
}else if(playerChoice==3&&computerChoice==3){
gameCount++;
System.out.println(“纸张对纸张!领带!\n”+
“玩家赢得了”+玩家+“时代和电脑”+
“已赢得”+计算机赢得”+“时代”);
}
//检查游戏计数是否达到最大游戏数,然后选择赢家
如果(gameCount==3&&computerWins>playerWins | | computerWins==2){
System.out.println(“计算机赢了!”);
打破
}否则如果(游戏计数=3&&computerWins
您的整个逻辑都在main()方法中。我建议您将while{}-part移动到一个新方法中,比如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(""));
}

HTH,SiS

您的整个逻辑都在main()方法中。我建议您将while{}-part移动到一个新方法中,比如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(""));
}

HTH,SiS

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

一个可能的解决方案是将游戏放入另一个while循环(包围gameCount的while循环)中,并在