Jump to content

6 posts in this topic

Posted

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.


[font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]import[/color][/font][/color][/font][font="Courier New"][color="#000000"] javax.swing.JOptionPane;[/color][/font]
[font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]import[/color][/font][/color][/font][font="Courier New"][color="#000000"] java.util.Random;[/color][/font]

[font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]public[/color][/font][/color][/font][font="Courier New"][color="#000000"] [/color][/font][font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]class[/color][/font][/color][/font][font="Courier New"][color="#000000"] RockPaperScissors
{ [/color][/font]
[font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]public[/color][/font][/color][/font][font="Courier New"][color="#000000"] [/color][/font][font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]static[/color][/font][/color][/font][font="Courier New"][color="#000000"] [/color][/font][font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]void[/color][/font][/color][/font][font="Courier New"][color="#000000"] main(String[] args)
{

JOptionPane.showMessageDialog([/color][/font][font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]null[/color][/font][/color][/font][font="Courier New"][color="#000000"], [/color][/font][font="Courier New"][color="#00cb00"][font="Courier New"][color="#00cb00"]"Let's play Rock Paper Scissors.\n1 = Rock\n2 = Paper\n3 = Scissors"[/color][/font][/color][/font][font="Courier New"][color="#000000"]);
String player;
player = JOptionPane.showInputDialog([/color][/font][font="Courier New"][color="#00cb00"][font="Courier New"][color="#00cb00"]"Please Enter a number between 1 and 3."[/color][/font][/color][/font][font="Courier New"][color="#000000"]);

Random generator = [/color][/font][font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]new[/color][/font][/color][/font][font="Courier New"][color="#000000"] Random();[/color][/font]
[font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]int[/color][/font][/color][/font][font="Courier New"][color="#000000"] computer = 1 + generator.nextInt(2);[/color][/font]

[font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]boolean[/color][/font][/color][/font][font="Courier New"][color="#000000"] 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);[/color][/font]

[font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]if[/color][/font][/color][/font][font="Courier New"][color="#000000"] (case1)
JOptionPane.showMessageDialog([/color][/font][font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]null[/color][/font][/color][/font][font="Courier New"][color="#000000"], [/color][/font][font="Courier New"][color="#00cb00"][font="Courier New"][color="#00cb00"]"Computer choses Paper. You lose."[/color][/font][/color][/font][font="Courier New"][color="#000000"]);[/color][/font]
[font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]else[/color][/font][/color][/font][font="Courier New"][color="#000000"] [/color][/font][font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]if[/color][/font][/color][/font][font="Courier New"][color="#000000"] (case2)
JOptionPane.showMessageDialog([/color][/font][font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]null[/color][/font][/color][/font][font="Courier New"][color="#000000"], [/color][/font][font="Courier New"][color="#00cb00"][font="Courier New"][color="#00cb00"]"Computer choses Scissors. You win."[/color][/font][/color][/font][font="Courier New"][color="#000000"]);[/color][/font]
[font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]else[/color][/font][/color][/font][font="Courier New"][color="#000000"] [/color][/font][font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]if[/color][/font][/color][/font][font="Courier New"][color="#000000"] (case3)
JOptionPane.showMessageDialog([/color][/font][font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]null[/color][/font][/color][/font][font="Courier New"][color="#000000"], [/color][/font][font="Courier New"][color="#00cb00"][font="Courier New"][color="#00cb00"]"Computer choses Rock. You win."[/color][/font][/color][/font][font="Courier New"][color="#000000"]);[/color][/font]
[font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]else[/color][/font][/color][/font][font="Courier New"][color="#000000"] [/color][/font][font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]if[/color][/font][/color][/font][font="Courier New"][color="#000000"] (case4)
JOptionPane.showMessageDialog([/color][/font][font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]null[/color][/font][/color][/font][font="Courier New"][color="#000000"], [/color][/font][font="Courier New"][color="#00cb00"][font="Courier New"][color="#00cb00"]"Computer choses Scissors. You lose."[/color][/font][/color][/font][font="Courier New"][color="#000000"]);[/color][/font]
[font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]else[/color][/font][/color][/font][font="Courier New"][color="#000000"] [/color][/font][font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]if[/color][/font][/color][/font][font="Courier New"][color="#000000"] (case5)
JOptionPane.showMessageDialog([/color][/font][font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]null[/color][/font][/color][/font][font="Courier New"][color="#000000"], [/color][/font][font="Courier New"][color="#00cb00"][font="Courier New"][color="#00cb00"]"Computer choses Rock. You lose."[/color][/font][/color][/font][font="Courier New"][color="#000000"]);[/color][/font]
[font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]else[/color][/font][/color][/font][font="Courier New"][color="#000000"] [/color][/font][font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]if[/color][/font][/color][/font][font="Courier New"][color="#000000"] (case6)
JOptionPane.showMessageDialog([/color][/font][font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]null[/color][/font][/color][/font][font="Courier New"][color="#000000"], [/color][/font][font="Courier New"][color="#00cb00"][font="Courier New"][color="#00cb00"]"Computer choses Paper. You win."[/color][/font][/color][/font][font="Courier New"][color="#000000"]);[/color][/font]
[font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]else[/color][/font][/color][/font][font="Courier New"][color="#000000"] [/color][/font][font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]if[/color][/font][/color][/font][font="Courier New"][color="#000000"] (case7)
JOptionPane.showMessageDialog([/color][/font][font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]null[/color][/font][/color][/font][font="Courier New"][color="#000000"], [/color][/font][font="Courier New"][color="#00cb00"][font="Courier New"][color="#00cb00"]"Both choose Rock. Tie."[/color][/font][/color][/font][font="Courier New"][color="#000000"]);[/color][/font]
[font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]else[/color][/font][/color][/font][font="Courier New"][color="#000000"] [/color][/font][font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]if[/color][/font][/color][/font][font="Courier New"][color="#000000"] (case8)
JOptionPane.showMessageDialog([/color][/font][font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]null[/color][/font][/color][/font][font="Courier New"][color="#000000"], [/color][/font][font="Courier New"][color="#00cb00"][font="Courier New"][color="#00cb00"]"Both choose Paper. Tie."[/color][/font][/color][/font][font="Courier New"][color="#000000"]);[/color][/font]
[font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]else[/color][/font][/color][/font][font="Courier New"][color="#000000"] [/color][/font][font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]if[/color][/font][/color][/font][font="Courier New"][color="#000000"] (case9)
JOptionPane.showMessageDialog([/color][/font][font="Courier New"][color="#941edf"][font="Courier New"][color="#941edf"]null[/color][/font][/color][/font][font="Courier New"][color="#000000"], [/color][/font][font="Courier New"][color="#00cb00"][font="Courier New"][color="#00cb00"]"Both choose Scissors. Tie."[/color][/font][/color][/font][font="Courier New"][color="#000000"]);

System.exit(0);
}
}[/color][/font]

Share this post


Link to post
Share on other sites

Posted

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

[font=Courier New][color=#000000]case1 = (player == 1) && (computer == 2);[/color][/font]

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted

[i][font="Courier New"]case1 = (player == 1) && (computer == 2);[/font][/i]

[i]Thank you but jgrasp still stops at case1. It now says[/i]
[i][font="CSD12"][color="#0000ff"][font="CSD12"][color="#0000ff"]

Share this post


Link to post
Share on other sites

Posted

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 [url="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Integer.html#parseInt(java.lang.String)"]Integer.parseInt[/url]

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.