Reyes
Reyes

Reputation: 19

Rock Paper Scissors question - only half return a result

I'm new to Java and was tasked with writing a two-player rock/paper/scissors game using nested if statements. Only half of my outcomes are printing results

The program works with all combinations when I use "else if ("rock".equals(player1) && "paper".equals(player2))" instead of the nested if statements, but having nested ifs is part of the rubric.

    Scanner in = new Scanner(System.in);

    System.out.print("Player 1, please choose rock, paper, or scissors: ");
    String player1 = in.next();         
    System.out.print("Player 2, please choose rock, paper, or scissors: ");
    String player2 = in.next(); 

    player1 = player1.toLowerCase();
    player2 = player2.toLowerCase();


    if (player1.equals(player2))
    {
        System.out.println("It's a tie!");
    }

    else if ("rock".equals(player1))    
    {
        if ("paper".equals(player2))
        {
            System.out.println("Paper covers rock - Player 2 wins!");
        }
    }

    else if ("rock".equals(player1))            
        {
        if ("scissors".equals(player2))     
        {
            System.out.println("Rock breaks scissors - Player 1 wins!");
        }
    }           

    else if ("paper".equals(player1))
    {
        if ("scissors".equals(player2))     
        {
            System.out.println("Scissors cut paper - Player 2 wins!");
        }
    }

    else if ("paper".equals(player1))
    {
        if ("rock".equals(player2))     
        {
            System.out.println("Paper covers rock - Player 1 wins!");
        }
    }       

    else if ("scissors".equals(player1))
    {
        if ("paper".equals(player2))        
        {
            System.out.println("Scissors cut paper - Player 1 wins!");
        }
    }
    else if ("scissors".equals(player1))
    {
        if ("rock".equals(player2))     
        {
            System.out.println("Rock breaks scissors - Player 2 wins!");
        }
    }

    else
    {
        System.out.println("Invalid input. Please try again using rock, paper, or scissors");
    }

    in.close();
    }

 }

I expected an output for each combination of inputs, but scissors vs rock, rock vs scissors, and paper vs rock don't yield any output to the console.

Upvotes: 0

Views: 74

Answers (1)

Code-Apprentice
Code-Apprentice

Reputation: 83527

else if ("rock".equals(player1))    
{
    if ("paper".equals(player2))
    {
        System.out.println("Paper covers rock - Player 2 wins!");
    }
}
else if ("rock".equals(player1))            
    {
    if ("scissors".equals(player2))     
    {
        System.out.println("Rock breaks scissors - Player 1 wins!");
    }
}  

Note that you have the same check twice. When the first one is true, then only that block will execute and the next else if... will be skipped. To fix this, just do all of the checks for player2 in one block:

else if ("rock".equals(player1))    
{
    if ("paper".equals(player2))
    {
        System.out.println("Paper covers rock - Player 2 wins!");
    }
    else if ("scissors".equals(player2))     
    {
        System.out.println("Rock breaks scissors - Player 1 wins!");
    }
}  

You should also learn about the switch statement which can provide an alternative solution to the problem.

Upvotes: 2

Related Questions