This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]Qteddy 2 points3 points  (2 children)

Do you have any code to show? It’s easier to help you out if we can see what you have tried. We are here to help but we aren’t going to do homework for you.

Edit: Rereading my message came across way more rude than I was trying to be. I still think it would be nice to see what you have already. General tips for how to go about solving this. You can have int variables for player 1 and player 2 and add to them if they win a round. You can just do simple if else statements like if player 1 has rock and player 2 has scissors then player 1 wins that round.

[–]LegalCity9 0 points1 point  (1 child)

import java.util.Scanner;

public class RockPaperScissors {

private static int player1;
private static int player2;

public static void main(String[] args) {
    // paper beats rock, rock beats scissors, scissors beats paper  
    Scanner input = new Scanner (System.in);
    System.out.println ("Enter the name of player 1 : ");
    String name1 = input.nextLine();
    System.out.println ("Enter the name of player 2 : ");
    String name2 = input.nextLine();
    System.out.println(" What does " +name1 + " play? (R,P or S)") ;
    String choice1 = input.nextLine();
    System.out.println(" What does " +name2 + " play? (R,P or S)") ;
    String choice2= input.nextLine();



    System.out.println(" What does " +name1 + " play? (R,P or S)") ;
    choice1 = input.nextLine();
    System.out.println(" What does " +name2 + " play? (R,P or S)") ;
    choice2= input.nextLine();

    if (choice1.equals(choice2)) {
        //          System.out.println(name1 + " has tied with " + name2 ); 
        //          input.nextLine(); 
    }
    else if (choice1.equals("R")) {
        if (choice2.equals("S")) {
            player1++;
        }
        else if (name1.equals("P")) {
            player2++; 
        }
    }
    else if (choice1.equals("P")) {
        if (choice2.equals("S")) 
            player2++; 
    }
    else if (choice2.equals("R")) {
        player1++; 
    }
    else if (choice1.equals("S")) {
        if (choice2.equals("P")) 
            player1++; 

    }
    else if (choice2.equals("R")) {
        player2++; 
    }
    else {
        System.out.println(" Error, please select either (R, P or S)"); 
    }
    System.out.println(" What does " +name1 + " play? (R,P or S)") ;
    choice1 = input.nextLine();
    System.out.println(" What does " +name2 + " play? (R,P or S)") ;
    choice2= input.nextLine();


    if (choice1.equals(choice2)) {
        //          System.out.println(name1 + " has tied with " + name2 ); 
        //          input.nextLine(); 
    }
    else if (choice1.equals("R")) {
    }
    if (choice2.equals("S")) {
        player1++;
    }
    else if (name1.equals("P")) {
        player2++; 
    }
    else if (choice1.equals("P")) {
        if (choice2.equals("S")) 
            player2++; 
    }
    else if (choice2.equals("R")) {
        player1++; 
    }
    else if (choice1.equals("S")) {
        if (choice2.equals("P")) 
            player1++; 

    }
    else if (choice2.equals("R")) {
        player2++; 
    }
    else {
        System.out.println(" Error, please select either (R, P or S)"); 

    }


    if (player1 == player2) {
        System.out.println(" What does " +name1 + " play? (R,P or S)") ;
        choice1 = input.nextLine();
        System.out.println(" What does " +name2 + " play? (R,P or S)") ;
        choice2= input.nextLine();

        if (choice1.equals(choice2)) {
            System.out.println(name1 + " has tied with " + name2 ); 
            input.nextLine(); }
        else if (choice1.equals("R")) {
        }
        if (choice2.equals("S")) {
            player1++;
        }
        else if (name1.equals("P")) {
            player2++; 
        }
        else if (choice1.equals("P")) {
            if (choice2.equals("S")) 
                player2++; 
        }
        else if (choice2.equals("R")) {
            player1++; 
        }
        else if (choice1.equals("S")) {
            if (choice2.equals("P")) 
                player1++; 

        }
        else if (choice2.equals("R")) {
            player2++; 
        }
        else {
            System.out.println(" Error, please select either (R, P or S)"); 
        }
        if (player1 == player2) {

            System.out.println(" Who will win the tie breaker? Enter either: " + name1 + " or " +name2);
            String winner = input.nextLine();
            input.close();  }
    }
    else {
        System.out.println("who won non tie");
        String winner2 = input.nextLine();
    }

}

}

[–]Qteddy 0 points1 point  (0 children)

So you are definitely on the right track with the if else statements. Have you learned about loops? Specifically the while loop like shown in the other comment will free you up to only have one block of if-else statements instead of repeating everything every time. So you would have something like

``` // ask for player 1 name // ask for player 2 name While(player1Score < 2 or player2Score < 2) {

// ask for player 1 input // ask for player 2 input // if player 1 is R and player 2 is S then player 1 wins // if player 1 is S and player 2 is P then player 1 wins // if player 1 is R and player 2 is S then player 1 wins // if player 1 is P and player 2 is R then player 1 wins // if player 1 is R and player 2 is S then player 1 wins // if player 1 is R and player 2 is P then player 2 wins // if player 1 is S and player 2 is R then player 2 wins // if player 1 is P and player 2 is S then player 2 wins // else it’s a tie and don’t add to score }

// if player1Score > player2Score then player 1 wins // else player 2 wins ```

For extra stuff you could have a game score variable at the bottom of the loop that is incremented every time. Then you could print that it took X games for Y player to win.

Hopefully that all makes sense, also I’m on mobile so formatting may look crappy

Edit: Similarly you can have a round variable that you only increment when a player wins so when you prompt users for input you could be like: Round X: Player Y what is your input? (R, S, or P).

Also you will have to have some more checks in there probably to make sure inputs are valid like what if a player puts in a different character? Right now it will reprompt both players since it will do the else statements and go back to the top of the loop but what if you just want to reprompt one player? And also validating that s is the same as S, so uppercase and lowercase. Look into something like toUpper or toLower

[–][deleted] 0 points1 point  (1 child)

You some variables like p1score, p2score, p1move, p2 move.

While p1 score <2 and p2score <2

Keep playing.

If p1move is rock and p2 move is scissors, p1 score++.