Click here to Skip to main content
15,923,142 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
Remember the childhood game “Rock, Paper, Scissors”? It is a two-player game in which each person simultaneously chooses either rock, paper, or scissors. Rock beats scissors but loses to paper, paper beats rock but loses to scissors, and scissors beats paper but loses to rock. The following code prompts player 1 and player 2 to each enter a string: rock, paper, or scissors. Finish the code by adding nested if statements to appropriately report “Player 1 wins”, “Player 2 wins”, or “It is a tie.”

so far this is the only thing i have, I'm clueless after this point...how do i compare strings if they aren't equal??
Java
import java.util.Scanner;
public class RockPaperScissors
{
     public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Player 1: Choose rock, scissors, or paper:";);
        String player1 = scan.next().toLowerCase();
        System.out.println("Player 2: Choose rock, scissors, or paper:");
        String player2 = scan.next().toLowerCase();

          if (player1.equals(player2))
          {
            System.out.print("It is a tie");
          }
        .....
    }
}
Posted
Updated 3-Mar-23 0:02am
v4

1 solution

I think a better aproach would be to treat the options as integers, it's easier to compare.
I would do something like this:

Java
final static int ROCK = 1, SCISSOR = 2, PAPER = 3;
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Player 1: Choose (1) - Rock, (2) - Scissors, or (3) - Paper: ");
        int player1 = scan.nextInt();
        System.out.println("Player 2: Choose (1) - Rock, (2) - Scissors, or (3) - Paper: ");
        int player2 = scan.nextInt();
     
        if (player1 == player2)
        {
            System.out.print("It is a tie");
        } else {
            switch (player1){
            case ROCK:
                if (player2 == SCISSOR)
                    System.out.print("Player 1 wins!");
                else
                    System.out.print("Player 2 wins!");
                break;
            case SCISSOR:
                if (player2 == PAPER)
                    System.out.print("Player 1 wins!");
                else
                    System.out.print("Player 2 wins!");
                break;
            case PAPER:
                if (player2 == ROCK)
                    System.out.print("Player 1 wins!");
                else
                    System.out.print("Player 2 wins!");
                break;
            }
        }
    }
 
Share this answer
 
v2
Comments
Richard MacCutchan 10-Jan-14 13:45pm    
Please don't do people's homework for them. It doesn't help.
Marcelo Camillo 10-Jan-14 13:49pm    
Sorry, then. I'm new here and was just trying to help.
Richard MacCutchan 10-Jan-14 14:01pm    
I'm sure you were, and that is commendable. But would you want to employ someone who gained their qualifications by submitting other people's work as their own?
Marcelo Camillo 10-Jan-14 14:05pm    
You're absolutelly right. I'll remember this next time.
lee11oh17 14-Jan-14 20:08pm    
I wasn't asking for complete code or for him to do my homework for me, I was just asking how to compare strings that aren't equal because that was the part I was having problems with. And I can't even use this code for the simple fact that it's not comparing strings, so he didn't do it for me, and in fact he didn't help me at all, though I do greatly appreciate the input! :)

And also Richard thank you for assuming I would claim Marcelo's work as my own, makes me feel great!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900