Click here to Skip to main content
14,452,932 members
Rate this:
Please Sign up or sign in to vote.
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??
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 31-Oct-19 6:14am
v4

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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

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;
            }
        }
    }
   
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!
Richard MacCutchan 15-Jan-14 4:27am
   
What do you mean by "how to compare strings that aren't equal"; comparing is used to determine that fact. And if you want to know the specifics about any Java class it is easy to find the documentation.
Marcelo Camillo 15-Jan-14 8:08am
   
Unless I'm not understanding exactly what you want, I think you're having problems with the logic. In my point of view, you have to deal with the possible cases in your code. As Richard said, you compare strings to know if they are equal or not.

The part of checking if both players choose the same option is right, but you have to define somewhere in your code what option is "stronger" than the other, so you can say who won.
H.Brydon 10-Jan-14 15:54pm
   
I somewhat agree with the code suggestion but use of enumerations would be better than int...

I also agree with RM's comments about doing homework...
CPallini 10-Jan-14 16:18pm
   
I somewhat agree but using enumeration would be better than use of an integer.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100