1

好的,所以我编写了一个简单的石头剪刀布游戏,使用用户输入来玩。用户输入他们的选择,随机函数选择计算机运行,然后产生结果。在此之后,程序终止,因为它已经完成。我想使用一个 while 循环,这样当游戏结束时,它会重新开始,然后如果用户输入exitquit程序将停止,这可以很容易地通过说诸如 while之类的话来完成playerGo != exit,play the game blah废话。但是,我无法让它工作,有人可以帮助我吗,我是 Java 菜鸟 :)

import java.util.Scanner;   

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

    Scanner input = new Scanner(System.in);

    int compGoInt;
    String compGo;
    String playerGo;

    System.out.println("You can type 'Exit' to quit the game at any time.");
    System.out.print("Please enter your choice. Rock, Paper or Scissors: ");
    playerGo = input.nextLine();

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

    switch (compGoInt){
    case 0:
        compGo = "Rock";
        break;
    case 1:
        compGo = "Paper";
        break;
    case 2:
        compGo = "Scissors";
        break;
    default:
        compGo = "Error";
        System.out.println("Error.");
        break;
    }

    if (playerGo.equals(compGo)){
        System.out.println("Computer chooses "+compGo);
        System.out.println("It's a draw!");
    }

    else if ((playerGo.equalsIgnoreCase("Rock") && compGo.equalsIgnoreCase("Scissors")) ||
            (playerGo.equalsIgnoreCase("Paper") && compGo.equalsIgnoreCase("Rock")) ||
            (playerGo.equalsIgnoreCase("Scissors") && compGo.equalsIgnoreCase("Paper"))){
        System.out.println("Computer chooses "+compGo);
        System.out.println("Player Wins!");
    }

    else if ((compGo.equalsIgnoreCase("Rock") && playerGo.equalsIgnoreCase("Scissors")) ||
            (compGo.equalsIgnoreCase("Paper") && playerGo.equalsIgnoreCase("Rock")) ||
            (compGo.equalsIgnoreCase("Scissors") && playerGo.equalsIgnoreCase("Paper"))){
        System.out.println("Computer chooses "+compGo);
        System.out.println("Computer Wins!");
    }

    else{
        System.out.println("Something has gone wrong!");
        System.out.println("Player chose "+playerGo);
        System.out.println("Computer chose "+compGo);
    }


}

}

4

3 回答 3

0

简单的说:

while(true) {
     if(playerGo.equalsIgnoreCase("Exit")) break;
     else //GameLogic
}

不过,如果我可以这样说,您应该让用户选择一个数字,因为任何字母输入都容易出错。

为澄清起见:

import java.util.Scanner;   

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

        Scanner input = new Scanner(System.in);

        int compGoInt;
        String compGo;
        String playerGo;

        while(true) {
            System.out.println("You can type 'Exit' to quit the game at any time.");
            System.out.print("Please enter your choice. Rock, Paper or Scissors: ");
            playerGo = input.nextLine();

            if(playerGo.equalsIgnoreCase("Exit")) break;    //Checks for exit condition.
            else { //GameLogic

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

                switch (compGoInt){
                case 0:
                    compGo = "Rock";
                    break;
                case 1:
                    compGo = "Paper";
                    break;
                case 2:
                    compGo = "Scissors";
                    break;
                default:
                    compGo = "Error";
                    System.out.println("Error.");
                    break;
                }

                if (playerGo.equals(compGo)){
                    System.out.println("Computer chooses "+compGo);
                    System.out.println("It's a draw!");
                }

                else if ((playerGo.equalsIgnoreCase("Rock") && compGo.equalsIgnoreCase("Scissors")) ||
                        (playerGo.equalsIgnoreCase("Paper") && compGo.equalsIgnoreCase("Rock")) ||
                        (playerGo.equalsIgnoreCase("Scissors") && compGo.equalsIgnoreCase("Paper"))){
                    System.out.println("Computer chooses "+compGo);
                    System.out.println("Player Wins!");
                }

                else if ((compGo.equalsIgnoreCase("Rock") && playerGo.equalsIgnoreCase("Scissors")) ||
                        (compGo.equalsIgnoreCase("Paper") && playerGo.equalsIgnoreCase("Rock")) ||
                        (compGo.equalsIgnoreCase("Scissors") && playerGo.equalsIgnoreCase("Paper"))){
                    System.out.println("Computer chooses "+compGo);
                    System.out.println("Computer Wins!");
                }

                else{
                    System.out.println("Something has gone wrong!");
                    System.out.println("Player chose "+playerGo);
                    System.out.println("Computer chose "+compGo);
                }
            }
        }
    }
}
于 2012-11-10T11:34:23.063 回答
0

您可以添加一个布尔变量 IsGameRunning = true。将整个登录包装在一个检查 IsGameRunning 状态的 while 循环中。在 switch 语句中添加一个 case 来检查用户输入“-1”,当用户输入“-1”作为输入时,case 会将 IsGameRunning 变量更改为 false。

简单,应该工作

于 2012-11-10T11:44:06.853 回答
0
boolean running = true;
do{
    System.out.println("You can type 'Exit' to quit the game at any time.");
    System.out.print("Please enter your choice. Rock, Paper or Scissors: ");
    playerGo = input.nextLine();
    running = !(playerGo.equalsIgnoreCase('Exit') || playerGo.equalsIgnoreCase('Quit'));
    if(running){
       //logic
       compGoInt = (int) (Math.random() * 3);
       ...
    }
}while(running)

do while -> input 需要至少被询问一次。

并且对于

if (playerGo.equals(compGo)){
     System.out.println("Computer chooses "+compGo);
     System.out.println("It's a draw!");
 }

playerGo.equals(compGo)->playerGo.equalsIgnoreCase(compGo)喜欢你的其他代码

于 2012-11-10T11:55:10.007 回答