0

I am creating a rock paper scissors game for class at school. I got everything to work, except for setting up something to restate the question when an invalid choice is answered. I figured a do...while statement was best here, but I realized that I am calling on a variable within the "do" part of the loop. So, since it's calling on something from another loop, it can't find that variable. Also, in my while statements, did I correctly list the variables that are acceptable? I'm pretty sure I just made that part up.

Thanks so much for your help guys!

import java.util.Scanner;

public class RockPaperScissorsTest {

    public static void main(String[] args) throws Exception {

        Scanner input = new Scanner(System. in );
        do {
            System.out.println("Player 1, choose 1 for rock, 2 for paper, or 3 for scissors.");
            int P1 = input.nextInt();
        } while (P != 1; P1 != 2; P1 != 3);
        System.out.println("");
        System.out.println("");
        System.out.println("");
        do {
            System.out.println("Player 2, choose 1 for rock, 2 for paper, or 3 for scissors.");
            int P2 = input.nextInt();
        } while (P2 != 1; P2 != 2; P2 != 3);
        if (P1 == 1 & P2 == 1)
            System.out.println("It's a tie!");
        if (P1 == 1 & P2 == 2)
            System.out.println("Player 2 wins!");
        if (P1 == 1 & P2 == 3)
            System.out.println("Player 1 wins!");
        if (P1 == 2 & P2 == 1)
            System.out.println("Player 1 wins!");
        if (P1 == 2 & P2 == 2)
            System.out.println("It's a tie!");
        if (P1 == 2 & P2 == 3)
            System.out.println("Player 2 wins!");
        if (P1 == 3 & P2 == 1)
            System.out.println("Player 2 wins!");
        if (P1 == 3 & P2 == 2)
            System.out.println("Player 1 wins");
        if (P1 == 3 & P2 == 3)
            System.out.println("It's a tie!");
    }
}
4
  • 1
    Step 1: Format your code legibly. :-) (I've done it for you on this occasion.) Commented Nov 5, 2013 at 11:09
  • Step 2, if P1 == P2, you lose 3 unnecessary conditions Commented Nov 5, 2013 at 11:09
  • What do you mean by "if P1==P2." Should I redo my conditions in the while statement? Commented Nov 5, 2013 at 11:12
  • 1
    P1 == P2 is the same thing like: P1 == 1 && P2 == 1, P1 == 2 && P2 == 2, P1 == 3 && P2 == 3 Commented Nov 5, 2013 at 11:19

3 Answers 3

3

Several things jump out:

  1. In Java (and most other languages syntactically derived from B), the logical AND operator is &&, not & (& is a mathematical operator, doing a bitwise boolean OR).

    So

    if (P1 == 1 && P2 == 1)
    //          ^^---- &&, not &
    
  2. In Java, variables have block scope. That means that a variable declared within a block is only available within that block (and any blocks it contains), not outside that block. So the P2 you're defining inside the loop here:

    do {
        System.out.println("Player 2, choose 1 for rock, 2 for paper, or 3 for scissors.");
        int P2 = input.nextInt();
    //  ^----------------------------- here
    } while (/*...*/);
    

    ...will only be available within the loop, not later after the loop. You need to declare that variable outside the loop. (And similarly for P1, of course.) In this case, the best place is the top of the function:

    int P1, P2;
    
  3. In any logical condition (including the condition of a while), to combine multiple criteria, you use logical operators && ("and") and || ("or"), not semicolons (;). So for instance:

    do {
        System.out.println("Player 2, choose 1 for rock, 2 for paper, or 3 for scissors.");
        P2 = input.nextInt();
    } while (P2 != 1 && P2 != 2 && P2 != 3);
    //               ^^---------^^---- "If P2 is not 1 AND P2 is not 2 AND P2 is not 3"
    
  4. You have three different tests to see if it's a tie, and just generally you're doing a lot of retesting of the same conditions (lots of P1 == 1, P2 == 2, etc.). You can test for a tie simply by checking if P1 == P2, and you can reduce the number of condition checks markedly by using else, for instance:

    if (P1 == P2) {
        System.out.println("It's a tie!");
    }
    else if (P1 == 1) {
        if (P2 == 2) {
            System.out.println("Player 2 wins!");
        }
        else {
            System.out.println("Player 1 wins!");
        }
    }
    else if (P1 == 2) {
        if (P2 == 1) {
            System.out.println("Player 1 wins!");
        }
        else {
            System.out.println("Player 2 wins!");
        }
    }
    else { // P1 == 3
        if (P2 == 1) {
            System.out.println("Player 2 wins!");
        }
        else {
            System.out.println("Player 1 wins");
        }
    }
    
  5. (More advanced) Look into enums. In my reworking your logic, I simply trusted that your logic was correct, because I have no idea what 1, 2, or 3 are when looking at the code. (Obviously higher up I could look at the message you output.) Using an enum would make the code a lot clearer. (if (P2 == RPS.Paper))

I suggest working through some guided Java tutorials. Oracle has several.

Sign up to request clarification or add additional context in comments.

2 Comments

My bad. Thanks so much. So, how do I call on P1 and P2 in the while statement since it is in the do loop?
@ZeroConnor: What do you mean by "call on"?
0

You need to declare the variables global. At the point where you declare the scanner. And only initialize it in the loop.

out of the loop:

int P1;

in the loop only do:

P1 = input.nextInt();

1 Comment

Thanks for the help guys, between this and T.J Crowder's first answer, I fixed my problem. You guys are awesome! Thanks for helping me clean up my program too!
0

As @TJ. Crowder told you, need to understand the ´AND´ (´&&´) operation and ´OR´ (´||´)

This is a compiling approach to your solution, but you need to improve the code to get it working. Hope you have a good time learning Java.

import java.util.Scanner;

public class RockPaperScissorsTest {

public static void main(String[] args) throws Exception {

    Scanner input = new Scanner(System. in );
    int P1 = 0;
    int P2 = 0;
    do {
        System.out.println("Player 1, choose 1 for rock, 2 for paper, or 3 for scissors.");
        P1 = input.nextInt();
    } while (P1 != 1 && P1 != 2 && P1 != 3);
    System.out.println("");
    System.out.println("");
    System.out.println("");
    do {
        System.out.println("Player 2, choose 1 for rock, 2 for paper, or 3 for scissors.");
        P2 = input.nextInt();
    } while (P2 != 1 && P2 != 2 && P2 != 3);
    if (P1 == 1 && P2 == 1)
        System.out.println("It's a tie!");
    if (P1 == 1 && P2 == 2)
        System.out.println("Player 2 wins!");
    if (P1 == 1 && P2 == 3)
        System.out.println("Player 1 wins!");
    if (P1 == 2 && P2 == 1)
        System.out.println("Player 1 wins!");
    if (P1 == 2 && P2 == 2)
        System.out.println("It's a tie!");
    if (P1 == 2 && P2 == 3)
        System.out.println("Player 2 wins!");
    if (P1 == 3 && P2 == 1)
        System.out.println("Player 2 wins!");
    if (P1 == 3 && P2 == 2)
        System.out.println("Player 1 wins");
    if (P1 == 3 && P2 == 3)
        System.out.println("It's a tie!");
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.