Total Noob
Total Noob

Reputation: 21

Rock, Paper, Scissors Java Programming Stuck

i'm new to Java Programming and I've looked through all the other questions and none of them seem to exactly help me with my dilemma. See, whenever I use my program all the answers show up and not just the right one. I don't know how to fix this. If anyone has advice please let me know. :) There is no computer in this, just two people or yourself. Thank you. I'm most likely overthinking this and it's going to end up being something very simple. haha.

      import java.util.*;

    public class RPS2 {

    public static void main(String[] args) {

        String p1;
        String p2;
        String user1 = "User 1";
        String user2 = "User 2";

        Scanner keyboard = new Scanner(System.in);

        System.out.println("Player 1, please enter your choice [P/R/S]");
        p1 = keyboard.nextLine();
        System.out.println("Player 2, please enter your choice [P/R/S]");
        p2 = keyboard.nextLine();

        if(user1.equalsIgnoreCase("p") && user2.equalsIgnoreCase("r"));
          System.out.println("Paper covers rock, Player 1 wins!");
        if(user1.equalsIgnoreCase("R") && user2.equalsIgnoreCase("S"));
          System.out.println("Paper covers rock, Player 2 wins!");

        if(user1.equalsIgnoreCase("p") && user2.equalsIgnoreCase("s"));
          System.out.println("Scissors cuts paper, Player 2 wins!");
        if(user1.equalsIgnoreCase("S") && user2.equalsIgnoreCase("P"));
          System.out.println("Scissors cuts paper, Player 1 wins!");

        if(user1.equalsIgnoreCase("r") && user2.equalsIgnoreCase("s"));
          System.out.println("Rock breaks scissors, Player 1 wins!");
        if(user1.equalsIgnoreCase("S") && user2.equalsIgnoreCase("R"));
          System.out.println("Rock breaks scissors, Player 2 wins!"); 

        if(user1.equalsIgnoreCase("user2"));
          System.out.println("Draw");
    }
}

Upvotes: 0

Views: 136

Answers (4)

David
David

Reputation: 69

All of your conditional lines have semicolons at the end

if(user1.equalsIgnoreCase("p") && user2.equalsIgnoreCase("r"));

remove the semicolons for all of them. I think that will fix.

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347194

You have a trailing ; at the end of each of your if conditions...

if(user1.equalsIgnoreCase("p") && user2.equalsIgnoreCase("r"));
                                                    ----------^

This means that the statement is essentially closed (or does nothing) and the line immediately following will executed regardless

Good practice would recommend that you always surround your if conditions in {...}, for example...

if(user1.equalsIgnoreCase("p") && user2.equalsIgnoreCase("r")) {
    System.out.println("Paper covers rock, Player 1 wins!");
}

I would also use if-else statements

if(user1.equalsIgnoreCase("p") && user2.equalsIgnoreCase("r")) {
    System.out.println("Paper covers rock, Player 1 wins!");
} else if(user1.equalsIgnoreCase("R") && user2.equalsIgnoreCase("S")) {
    System.out.println("Paper covers rock, Player 2 wins!");
} ...

This will further decrease the potential for you to accidentally have more then on win condition (but I would structure it differently, but that's just me ;))

You're also comparing the wrong values (thanks John3136 +1)

String p1;
String p2;
String user1 = "User 1";
String user2 = "User 2";
//...
System.out.println("Player 1, please enter your choice [P/R/S]");
p1 = keyboard.nextLine();
System.out.println("Player 2, please enter your choice [P/R/S]");
p2 = keyboard.nextLine();

 if(p1.equalsIgnoreCase("p") && p2.equalsIgnoreCase("r")) {
    System.out.println("Paper covers rock, Player 1 wins!");
} else if(p1.equalsIgnoreCase("R") && p2.equalsIgnoreCase("S")) {
    System.out.println("Paper covers rock, Player 2 wins!");
} ...

Upvotes: 2

John3136
John3136

Reputation: 29265

You put the answers in p1 and p2 but then compare user1 and user2. So you aren't actually comparing the user input.

And as @andreicovaciu points out, you have ; after each if that you need to remove.

Upvotes: 3

andreicovaciu
andreicovaciu

Reputation: 354

After each if you put there a semicolon

Upvotes: 0

Related Questions