2 Replies - 592 Views - Last Post: 24 October 2012 - 11:50 AM Rate Topic: -----

#1 fantasyphil  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-October 12

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!



 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");
			}
		}
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Help with Rock, Paper, Scissors code

#2 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

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.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1