Kel Okekpe Kel Okekpe - 22 days ago 10
Java Question

Incorporating While Loop Into Program

I am creating a program that allows you to play a game similar to paper, scissors rock.

The game works, but I am trying to incorporate a loop into the code that will ask the user if they wish to continue playing.

If yes, it will ask them for another input.
If no, the program will simply state "Thanks for playing"

Here's the code:

import java.util.Scanner;
import java.util.Random;
public class OkekpeJMoropinzee
{
public static void main(String[]args)
{

String yourMove;
String compMove;


int compInt;





String[] characters = {"Monkey","Robot","Pirate","Ninja","Zombie"};

Scanner input = new Scanner(System.in);
Random rand = new Random(6);

compInt = rand.nextInt(5)+1;

if (compInt == 1)
compMove = "Monkey";
else if (compInt == 2)
compMove = "Robot";
else if (compInt == 3)
compMove = "Pirate";
else if (compInt == 4)
compMove = "Ninja";
else if (compInt == 5)
compMove = "Zombie";




System.out.println("What do you choose?: ");
yourMove = input.next();
//MONKEY
if(yourMove == "Monkey" || compInt == 1)
System.out.println("Tie");
else if (yourMove== "Monkey" || compInt == 2)
System.out.println("You Win! Monkey Unplugs Robot!");
else if (yourMove=="Monkey" || compInt == 3)
System.out.println("You Lose! Pirate Skewers Monkey!");
else if (yourMove == "Monkey" || compInt==4)
System.out.println("You Win! Monkey fools Ninja!");
else if (yourMove== "Monkey" || compInt==5)
System.out.println("You Lose! Zombie savages monkey!");

//RoBOT
else if(yourMove == "Robot" || compInt == 2)
System.out.println("Tie");
else if (yourMove== "Robot" || compInt == 1)
System.out.println("You Lose! Monkey Unplugs Robot!");
else if (yourMove=="Robot" || compInt == 3)
System.out.println("You Lose! Pirate Drowns Robot!!");
else if (yourMove == "Robot" || compInt==4)
System.out.println("You Win! Robot Chokes Ninja");
else if (yourMove== "Robot" || compInt==5)
System.out.println("You win! Robot Crushes Zombie!");

//PIRATE
else if(yourMove == "Pirate" || compInt == 3)
System.out.println("Tie");
else if (yourMove== "Pirate" || compInt == 1)
System.out.println("You Win! Pirate Skewers Monkey!");
else if (yourMove=="Pirate" || compInt == 2)
System.out.println("You Win! Pirate Drowns Robot!");
else if (yourMove == "Pirate" || compInt==4)
System.out.println("You Lose! Ninja Karate Chops Pirate!");
else if (yourMove== "Pirate" || compInt==5)
System.out.println("You Lose! Zombie Eats Pirate!");

//NINJA
else if(yourMove == "Ninja" || compInt == 4)
System.out.println("Tie");
else if (yourMove== "Ninja" || compInt == 1)
System.out.println("You Lose! Monkey Fools Ninja!");
else if (yourMove=="Ninja" || compInt == 2)
System.out.println("You Lose! Robot Chokes Ninja!");
else if (yourMove == "Ninja" || compInt==3)
System.out.println("You Win! Ninja Karate Chops Pirate!");
else if (yourMove== "Ninja" || compInt==5)
System.out.println("You Win! Ninja Decapitates Zombie!");

//ZOMBIE
else if(yourMove == "Zombie" || compInt == 5)
System.out.println("Tie");
else if (yourMove== "Zombie" || compInt == 1)
System.out.println("You Win! Zombie Savages Monkey!");
else if (yourMove=="Zombie" || compInt == 2)
System.out.println("You Lose! Robot Crushes Zombie!");
else if (yourMove == "Zombie" || compInt==3)
System.out.println("You Win! Zombie Eats Pirate!");
else if (yourMove== "Zombie" || compInt==4)
System.out.println("You Lose! Ninja Decapitates Zombie!");
}
}

Answer

You could do something like this:

import java.util.Scanner;
import java.util.Random;
public class OkekpeJMoropinzee
{
    public static void main(String[]args)
    {
        String playAgain; 
    do{
        String yourMove;
        String compMove;


        int compInt;





        String[] characters = {"Monkey","Robot","Pirate","Ninja","Zombie"};

        Scanner input = new Scanner(System.in);
        Random rand = new Random(6);

        compInt = rand.nextInt(5)+1;

        if (compInt == 1) 
           compMove = "Monkey"; 
        else if (compInt == 2) 
           compMove = "Robot"; 
        else if (compInt == 3) 
           compMove = "Pirate"; 
        else if (compInt == 4)
            compMove = "Ninja";
        else if (compInt == 5)
            compMove = "Zombie";




        System.out.println("What do you choose?: "); 
        yourMove = input.next();
        //MONKEY
        if(yourMove == "Monkey" || compInt == 1)
            System.out.println("Tie");
        else if (yourMove== "Monkey" || compInt == 2)
            System.out.println("You Win! Monkey Unplugs Robot!");
        else if (yourMove=="Monkey" || compInt == 3)
            System.out.println("You Lose! Pirate Skewers Monkey!");
        else if (yourMove == "Monkey" || compInt==4)
            System.out.println("You Win! Monkey fools Ninja!");
        else if (yourMove== "Monkey" || compInt==5)
            System.out.println("You Lose! Zombie savages monkey!");

        //RoBOT
        else if(yourMove == "Robot" || compInt == 2)
            System.out.println("Tie");
        else if (yourMove== "Robot" || compInt == 1)
            System.out.println("You Lose! Monkey Unplugs Robot!");
        else if (yourMove=="Robot" || compInt == 3)
            System.out.println("You Lose! Pirate Drowns Robot!!");
        else if (yourMove == "Robot" || compInt==4)
            System.out.println("You Win! Robot Chokes Ninja");
        else if (yourMove== "Robot" || compInt==5)
            System.out.println("You win! Robot Crushes Zombie!");

        //PIRATE
        else if(yourMove == "Pirate" || compInt == 3)
            System.out.println("Tie");
        else if (yourMove== "Pirate" || compInt == 1)
            System.out.println("You Win! Pirate Skewers Monkey!");
        else if (yourMove=="Pirate" || compInt == 2)
            System.out.println("You Win! Pirate Drowns Robot!");
        else if (yourMove == "Pirate" || compInt==4)
            System.out.println("You Lose! Ninja Karate Chops Pirate!");
        else if (yourMove== "Pirate" || compInt==5)
            System.out.println("You Lose! Zombie Eats Pirate!");

        //NINJA
        else if(yourMove == "Ninja" || compInt == 4)
            System.out.println("Tie");
        else if (yourMove== "Ninja" || compInt == 1)
            System.out.println("You Lose! Monkey Fools Ninja!");
        else if (yourMove=="Ninja" || compInt == 2)
            System.out.println("You Lose! Robot Chokes Ninja!");
        else if (yourMove == "Ninja" || compInt==3)
            System.out.println("You Win! Ninja Karate Chops Pirate!");
        else if (yourMove== "Ninja" || compInt==5)
            System.out.println("You Win! Ninja Decapitates Zombie!");

        //ZOMBIE
        else if(yourMove == "Zombie" || compInt == 5)
            System.out.println("Tie");
        else if (yourMove== "Zombie" || compInt == 1)
            System.out.println("You Win! Zombie Savages Monkey!");
        else if (yourMove=="Zombie" || compInt == 2)
            System.out.println("You Lose! Robot Crushes Zombie!");
        else if (yourMove == "Zombie" || compInt==3)
            System.out.println("You Win! Zombie Eats Pirate!");
        else if (yourMove== "Zombie" || compInt==4)
            System.out.println("You Lose! Ninja Decapitates Zombie!");

        System.out.println("Would you like to play again? Type yes to play again."); 
        playAgain = input.next();
        } while(playAgain.equals("yes"));
    }

}
Comments