Jump to content



Photo

Need Help With Java Rock Paper Sissors Code


  • Please log in to reply
5 replies to this topic

#1 The Real Napster

The Real Napster

    Neowinian

  • Joined: 11-July 07
  • Location: GA

Posted 20 January 2013 - 21:12

I am writting a rock paper scissors program for my java class. I have wrote a program that should work but i keep having trouble with getting my variables to work write. Sometimes compiler will read them and sometimes it wont. I am using jgrasp to write with. Any help would be greatly appreciated. Also this is my first time using java so i know there are some mistakes. I just cant find them.


import javax.swing.JOptionPane;
import java.util.Random;

public class RockPaperScissors
{

public static void main(String[] args)
{

JOptionPane.showMessageDialog(
null, "Let's play Rock Paper Scissors.\n1 = Rock\n2 = Paper\n3 = Scissors");
String player;
player = JOptionPane.showInputDialog(
"Please Enter a number between 1 and 3.");

Random generator =
new Random();
int computer = 1 + generator.nextInt(2);

boolean case1, case2, case3, case4, case5, case6, case7, case8, case9;

case1 = (player = 1) && (computer = 2);
case2 = (player = 1) && (computer = 3);
case3 = (player = 2) && (computer = 1);
case4 = (player = 2) && (computer = 3);
case5 = (player = 3) && (computer = 1);
case6 = (player = 3) && (computer = 2);
case7 = (player = 1) && (computer = 1);
case8 = (player = 2) && (computer = 2);
case9 = (player = 3) && (computer = 3);


if (case1)
JOptionPane.showMessageDialog(
null, "Computer choses Paper. You lose.");
else if (case2)
JOptionPane.showMessageDialog(
null, "Computer choses Scissors. You win.");
else if (case3)
JOptionPane.showMessageDialog(
null, "Computer choses Rock. You win.");
else if (case4)
JOptionPane.showMessageDialog(
null, "Computer choses Scissors. You lose.");
else if (case5)
JOptionPane.showMessageDialog(
null, "Computer choses Rock. You lose.");
else if (case6)
JOptionPane.showMessageDialog(
null, "Computer choses Paper. You win.");
else if (case7)
JOptionPane.showMessageDialog(
null, "Both choose Rock. Tie.");
else if (case8)
JOptionPane.showMessageDialog(
null, "Both choose Paper. Tie.");
else if (case9)
JOptionPane.showMessageDialog(
null, "Both choose Scissors. Tie.");

System.exit(0);
}
}



#2 dnast

dnast

    Long-winded

  • Joined: 07-January 02
  • Location: Location Location

Posted 20 January 2013 - 21:18

You should be using '==' instead of '=' to compare values.
For example:

case1 = (player == 1) && (computer == 2);

#3 Rohdekill

Rohdekill

    Neowinian Senior

  • Tech Issues Solved: 1
  • Joined: 06-July 05
  • Location: Earth

Posted 20 January 2013 - 21:29

You also don't need an "Else case 9". Just use "Else" as there would be no alternatives left but 9.

#4 OP The Real Napster

The Real Napster

    Neowinian

  • Joined: 11-July 07
  • Location: GA

Posted 20 January 2013 - 21:29

case1 = (player == 1) && (computer == 2);

Thank you but jgrasp still stops at case1. It now says
§ÏRockPaperScissors.java:18: error: incomparable types: String and int
ÏÏ§Ï case1 = (player == 1) && (computer == 2);
ÏÏ§Ï ^


#5 Duality

Duality

    Neowinian

  • Joined: 10-March 07
  • Location: Liverpool, UK
  • OS: Windows 8 Pro x64
  • Phone: LG Nexus 4

Posted 20 January 2013 - 21:38

The human player is providing a number when asked - but the data type returned by that function is actually a String. You need to convert that string into an Integer to successfully compare it to the computer's selection (which is an integer).

See Integer.parseInt

#6 OP The Real Napster

The Real Napster

    Neowinian

  • Joined: 11-July 07
  • Location: GA

Posted 20 January 2013 - 21:45

Thank Yall for the help. I got it to work. Now just got to redo some wording