Note that there are some explanatory texts on larger screens.

plurals
  1. POCall on a variable created in another loop in Java, and how to have multiple conditions in a do...while loop
    primarykey
    data
    text
    <p>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.</p> <p>Thanks so much for your help guys!</p> <pre><code>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 &amp; P2 == 1) System.out.println("It's a tie!"); if (P1 == 1 &amp; P2 == 2) System.out.println("Player 2 wins!"); if (P1 == 1 &amp; P2 == 3) System.out.println("Player 1 wins!"); if (P1 == 2 &amp; P2 == 1) System.out.println("Player 1 wins!"); if (P1 == 2 &amp; P2 == 2) System.out.println("It's a tie!"); if (P1 == 2 &amp; P2 == 3) System.out.println("Player 2 wins!"); if (P1 == 3 &amp; P2 == 1) System.out.println("Player 2 wins!"); if (P1 == 3 &amp; P2 == 2) System.out.println("Player 1 wins"); if (P1 == 3 &amp; P2 == 3) System.out.println("It's a tie!"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload