user7154937
user7154937

Reputation:

Paper Scissors Rock not working

I am creating a code that works similar to paper scissors rock.

I created a random number generator that allows the computer to randomly generate a number between 1-5, each number represents a different character (monkey, robot, pirate, ninja zombie)

The user is asked to input a number between 1-5 and the code is suppose to compare the User input and the Randomly Generated number and inform the user who won.

for ex. 1(monkey) beats 4(ninja); so if the user chooses 1, and the computer randomly generates 4, the words "you win, the monkey fools the ninja!" should be displayed.

for some reason my code only displays "Tie" when i enter 1, and "You win, monkey unplugs robot" for any other value I enter.

It seems that the random number generator is not working correctly.

can someone please help me with this issue?

import java.util.Scanner;
    import java.util.Random;
    public class OkekpeJMoropinzee
    {
        public static void main(String[]args)
        {
            String playAgain; 
        do{
            int 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.nextInt();
            //MONKEY
            if(yourMove == 1 || compInt == 1)
              System.out.println("Tie");
            else if (yourMove== 1 || compInt == 2)
                System.out.println("You Win! Monkey Unplugs Robot!");
            else if (yourMove== 1 || compInt == 3)
                System.out.println("You Lose! Pirate Skewers Monkey!");
            else if (yourMove == 1 || compInt == 4)
                System.out.println("You Win! Monkey fools Ninja!");
            else if (yourMove== 1 || compInt == 5)
                System.out.println("You Lose! Zombie savages monkey!");

Upvotes: 0

Views: 73

Answers (1)

user5478656
user5478656

Reputation: 276

You've given a OR operator in your if statement.

if(yourMove == 1 || compInt == 1)

When you enter 1, the first if condition would pass irrespective of the "compInt " value.

You should be using AND (&&) operator.

       if(yourMove == 1 && compInt == 1)
          System.out.println("Tie");
        else if (yourMove== 1 && compInt == 2)
            System.out.println("You Win! Monkey Unplugs Robot!");
        else if (yourMove== 1 && compInt == 3)
            System.out.println("You Lose! Pirate Skewers Monkey!");
        else if (yourMove == 1 && compInt == 4)
            System.out.println("You Win! Monkey fools Ninja!");
        else if (yourMove== 1 && compInt == 5)
            System.out.println("You Lose! Zombie savages monkey!");

Upvotes: 0

Related Questions