我正在制作一个石头剪刀游戏,一旦计算机或用户有一个最好的2/3然后它应该提示他们是否想再次玩游戏。如果“是”那么程序重新开始,如果“否”,那么system.exit我想。
我应该怎么做?
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!");
}
}
}
答案 0 :(得分:3)
你的整个逻辑都在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
答案 1 :(得分:0)
一种可能的解决方案是将游戏置于另一个while循环(包含gameCount的循环)中,并在while循环中为变量创建一个switch语句,让我们说gameContinue
。在游戏结束时,用户将收到提示是否继续。根据他们的答案,您可以更改变量gameContinue
的值,然后就可以了。