我面临的一个问题是,如果用户想再次玩游戏,它不会提示用户键入数字,而是会打印出语句,我想知道为什么要这样做...如果用户输入“否”,它将完美运行,游戏将结束。我将提供图片,以便您可以更好地了解我的问题。
import java.util.Scanner;
import java.util.*;
public class Moropinzee
{
public static void main(String[] args) {
int Monkey = 1;
int Robot = 2;
int Pirate = 3;
int Ninja = 4;
int Zombie = 5;
int player1 = 0;
int player2 = 0;
String answer;
Scanner scan = new Scanner(System.in);
System.out.print("Hey, let's play Moropinzee!\n" +
"Please enter a move.\n");
do{
System.out.println("Player 1: Enter a number 1-5 for Monkey, Robot, Pirate, Ninja, or Zombie:");
while(!(player1>0) || !(player1<6))
{
player1 = scan.nextInt();
if(player1>=6)
System.out.println("Invalid choice, Player 1. Enter a number 1-5:");
}
Scanner keyboard = new Scanner(System.in);
System.out.println("Player 2: Enter a number 1-5 for Monkey, Robot, Pirate, Ninja, or Zombie:");
while(!(player2>0) || !(player2<6))
{
player2 = scan.nextInt();
if(player2>=6)
System.out.println("Invalid choice, Player 2. Enter a number 1-5:");
}
if(player1==(player2)){
System.out.println("Nobody wins!");
}
else if (player1==(1)){
if (player2==(4)){
System.out.println("Monkey fools Ninja! Player 1 Wins!");
}
}
else if (player2==(1)){
if (player1==(4)){
System.out.println("Monkey fools Ninja! Player 2 Wins! ");
}
}
if (player1==(1)){
if (player2==(2))
System.out.println("Monkey unplugs Robot! Player 1 Wins!");
}
if (player2==(1)){
if (player1==(2))
System.out.println("Monkey unplugs Robot! Player 2 Wins!");
}
if (player1==(2)){
if (player2==(4))
System.out.println("Robot chokes Ninja! Player 1 Wins!");
}
if (player1==(4)){
if (player2==(2))
System.out.println("Robot chokes Ninja! Player 2 Wins!");
}
if (player1==(2)){
if (player2==(5))
System.out.println("Robot crushes Zombie! Player 1 Wins!");
}
if (player1==(5)){
if (player2==(2))
System.out.println("Robot crushes Zombie! Player 2 Wins!");
}
if (player1==(3)){
if (player2==(2))
System.out.println("Pirate drowns Robot! Player 1 Wins!");
}
if (player1==(2)){
if (player2==(3))
System.out.println("Pirate drowns Robot! Player 2 Wins!");
}
if (player1==(3)){
if (player2==(1))
System.out.println("Pirate skewers Monkey! Player 1 Wins!");
}
if (player1==(1)){
if (player2==(3))
System.out.println("Pirate skewers Monkey! Player 2 Wins!");
}
if (player1==(4)){
if (player2==(3))
System.out.println("Ninja karate chops Pirate! Player 1 Wins!");
}
if (player1==(3)){
if (player2==(4))
System.out.println("Ninja karate chops Pirate! Player 2 Wins!");
}
if (player1==(4)){
if (player2==(5))
System.out.println("Ninja decapitates Zombie! Player 1 Wins!");
}
if (player1==(5)){
if (player2==(4))
System.out.println("Ninja decapitates Zombie! Player 2 Wins!");
}
if (player1==(5)){
if (player2==(3))
System.out.println("Zombie eats Pirate! Player 1 Wins!");
}
if (player1==(3)){
if (player2==(5))
System.out.println("Zombie eats Pirate! Player 2 Wins!");
}
if (player1==(5)){
if (player2==(1))
System.out.println("Zombie savages Monkey! Player 1 Wins!");
}
if (player1==(1)){
if (player2==(5))
System.out.println("Zombie savages Monkey! Player 2 Wins!");
}
System.out.println("Do you want to play again? Yes or No");
answer = keyboard.next();
}
while(answer.equalsIgnoreCase("Yes"));
}
}
答案 0 :(得分:1)
问题在于,第一次迭代后就永远不会重置变量。在循环中,仅在player1
和player2
变量不在有效范围内时才对其进行修改,它们将在以下迭代中出现。因此,只要不断运行,每次迭代都将与第一次迭代相同。尝试将代码更改为:
player1 = 0;
player2 = 0;
System.out.println("Do you want to play again? Yes or No");
answer = keyboard.next();