import java.util.*;
import javax.swing.JOptionPane;
public class RockPaper
{
public static void main (String [] args)
{
Random rg = new Random();
String user = "Human";
String comp = "Computer";
int num=0;
while (num==0)
{
int cc=(rg.nextInt(3));
if (cc==0)
{
comp = "Rock";
}
else if (cc==1)
{
comp = "Paper";
}
else if (cc==2)
{
comp = "Scissors";
}
user = JOptionPane.showInputDialog("Rock, Paper, or Scissors?");//user choice and input
if (user==comp)
{
num=JOptionPane.showConfirmDialog(null,"You Chose: "+user+"\nThe Computer Chose: "+comp+"\nIt is a Tie!"+"\nWould You Like to Play Again?");
}
else if (user=="Rock"&comp=="Scissors")
{
num=JOptionPane.showConfirmDialog(null,"You Chose: "+user+"\nThe Computer Chose: "+comp+"\nYou Win!"+"\nWould You Like to Play Again?");
}
else if (user=="Rock"&comp=="Paper")
{
num=JOptionPane.showConfirmDialog(null,"You Chose: "+user+"\nThe Computer Chose: "+comp+"\nYou Lose!"+"\nWould You Like to Play Again?");
}
else if (user=="Paper"&comp=="Rock")
{
num=JOptionPane.showConfirmDialog(null,"You Chose: "+user+"\nThe Computer Chose: "+comp+"\nYou Win!"+"\nWould You Like to Play Again?");
}
else if (user=="Paper"&comp=="Scissors")
{
num=JOptionPane.showConfirmDialog(null,"You Chose: "+user+"\nThe Computer Chose: "+comp+"\nYou Lose!"+"\nWould You Like to Play Again?");
}
else if (user=="Scissors"&comp=="Paper")
{
num=JOptionPane.showConfirmDialog(null,"You Chose: "+user+"\nThe Computer Chose: "+comp+"\nYou Win!"+"\nWould You Like to Play Again?");
}
else if (user=="Scissors"&comp=="Rock")
{
num=JOptionPane.showConfirmDialog(null,"You Chose: "+user+"\nThe Computer Chose: "+comp+"\nYou Lose!"+"\nWould You Like to Play Again?");
}
else
{
System.out.println("error");
}
}
}
}
Help with Rock, Paper, Scissors code
Page 1 of 12 Replies - 334 Views - Last Post: 24 October 2012 - 11:50 AM
#1
Help with Rock, Paper, Scissors code
Posted 23 October 2012 - 07:13 PM
I am trying to get a rock, paper, scissors java code to work and every time I keep getting the error else in my while loop. why is this? How can I fix the loop to give me the correct output? Thanks!
Replies To: Help with Rock, Paper, Scissors code
#2
Re: Help with Rock, Paper, Scissors code
Posted 23 October 2012 - 07:48 PM
In your if-else statement below, change all the == to .equals so you can get the proper comparison you want.
#3
Re: Help with Rock, Paper, Scissors code
Posted 24 October 2012 - 11:50 AM
You can't compare String or any other oObject using the == operator
You'll need to use the equals() method
You'll need to use the equals() method
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|