Reputation: 1
I am facing with an issue where if a user want to play the game again, it will not prompt the user to key in the numbers, instead, it would print out statements and I am wondering why is doing that... It works perfectly fine if a user types in "No" and the game will end. I will provide a picture down so you can have a better understanding of what my issue is.
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"));
}
}
https://i.sstatic.net/64H22.png
Upvotes: 0
Views: 58
Reputation: 12819
The problem is that you never reset your variables after the first iteration. In your loop you only modify the player1
and player2
variables if they aren't within the valid range, which they will be in the following iterations. Thus every iteration will be the same as the first as long as it keeps running. Try changing your code to:
player1 = 0;
player2 = 0;
System.out.println("Do you want to play again? Yes or No");
answer = keyboard.next();
Upvotes: 1