Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Several things jump out:</p> <ol> <li><p>In Java (and most other languages syntactically derived from B), the <em>logical</em> AND operator is <code>&amp;&amp;</code>, not <code>&amp;</code> (<code>&amp;</code> is a mathematical operator, doing a bitwise boolean OR).</p> <p>So</p> <pre><code>if (P1 == 1 &amp;&amp; P2 == 1) // ^^---- &amp;&amp;, not &amp; </code></pre></li> <li><p>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 <em>outside</em> that block. So the <code>P2</code> you're defining inside the loop here:</p> <pre><code>do { System.out.println("Player 2, choose 1 for rock, 2 for paper, or 3 for scissors."); int P2 = input.nextInt(); // ^----------------------------- here } while (/*...*/); </code></pre> <p>...will only be available <em>within</em> the loop, not later after the loop. You need to declare that variable outside the loop. (And similarly for <code>P1</code>, of course.) In this case, the best place is the top of the function:</p> <pre><code>int P1, P2; </code></pre></li> <li><p>In any logical condition (including the condition of a <code>while</code>), to combine multiple criteria, you use logical operators <code>&amp;&amp;</code> ("and") and <code>||</code> ("or"), not semicolons (<code>;</code>). So for instance:</p> <pre><code>do { System.out.println("Player 2, choose 1 for rock, 2 for paper, or 3 for scissors."); P2 = input.nextInt(); } while (P2 != 1 &amp;&amp; P2 != 2 &amp;&amp; P2 != 3); // ^^---------^^---- "If P2 is not 1 AND P2 is not 2 AND P2 is not 3" </code></pre></li> <li><p>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 <code>P1 == 1</code>, <code>P2 == 2</code>, etc.). You can test for a tie simply by checking if <code>P1 == P2</code>, and you can reduce the number of condition checks markedly by using <code>else</code>, for instance:</p> <pre><code>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"); } } </code></pre></li> <li><p><em>(More advanced)</em> Look into <code>enum</code>s. In my reworking your logic, I simply trusted that your logic was correct, because I have no idea what <code>1</code>, <code>2</code>, or <code>3</code> are when looking at the code. (Obviously higher up I could look at the message you output.) Using an <code>enum</code> would make the code a lot clearer. (<code>if (P2 == RPS.Paper)</code>)</p></li> </ol> <p>I suggest working through some guided Java tutorials. Oracle <a href="http://docs.oracle.com/javase/tutorial/" rel="nofollow">has several</a>.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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