Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Else If Block Not Working Correctly
    primarykey
    data
    text
    <p>Hopefully this is a quick question. I'm making a simple rock paper scissors game. I make a random computer choice fine, and get the user's choice fine. But when I try to find who wins, it prints the last else in my else if block that is for invalid input.</p> <p>It prints "Enter a valid choice" when a correct choice has been made. </p> <pre><code>import java.util.Random; import javax.swing.JOptionPane; public class JavaApplication4 { public static void main(String[] args) { Random ranNums = new Random(); int comp = ranNums.nextInt(3); String comp2; String winner; String user = JOptionPane.showInputDialog (null, "Enter rock, paper, or scissors"); user.toLowerCase(); if(comp == 0) comp2 = "rock"; else if(comp == 1) comp2 = "paper"; else comp2 = "scissors"; //Computer wins if(comp2 == "rock" &amp;&amp; user == "scissors") winner = "The computer wins"; else if(comp2 == "paper" &amp;&amp; user == "rock") winner = "The computer wins"; else if(comp2 == "scissors" &amp;&amp; user == "paper") winner = "The computer wins"; //Tie game else if(comp2 == "rock" &amp;&amp; user == "rock") winner = "It's a tie"; else if(comp2 == "paper" &amp;&amp; user == "paper") winner = "It's a tie"; else if(comp2 == "scissors" &amp;&amp; user == "scissors") winner = "It's a tie"; //User wins else if(comp2 == "scissors" &amp;&amp; user == "rock") winner = "You win!"; else if(comp2 == "rock" &amp;&amp; user == "paper") winner = "You win!"; else if(comp2 == "paper" &amp;&amp; user == "scissors") winner = "You win!"; else winner = "Enter a valid choice"; JOptionPane.showMessageDialog(null, "You picked " + user + "\n" + "The computer picked " + comp2 + "\n" + winner); } } </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