aspose file tools*
The moose likes Beginning Java and the fly likes Simulate a game of Rock, Paper, Scissors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Simulate a game of Rock, Paper, Scissors" Watch "Simulate a game of Rock, Paper, Scissors" New topic
Author

Simulate a game of Rock, Paper, Scissors

Todd Smith Todd
Greenhorn

Joined: Dec 11, 2002
Posts: 4
Code for the game on the Java Ranch web site has this code

if(p1Choice==p2Choice)
{
draw++;
System.out.println("\n\t\t\t Draw \n");
}

I'm not sure if I understand or not. In effect with the "==" you are comparing the address at which the strings are stored. Unless they are exactly the same string (that is, at the same address, not that they have the same content) this will always fail.

I am trying to change the program and use p1Choice = (inData.readLine()); and p1Choice = (inData.readLine()); I enter the same for both and they never match. I am just trying to understand. Sorry I am new to Java and I just would like to understand
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Assuming that p1Choice and p2Choice are String objects (or any type of object for that matter), you are exactly right. You should use p1Choice.equals(p2Choice) instead.

Layne
[ October 27, 2004: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
haven't looked at the specific code but could be something like this.

You have three constants
ROCK
PAPER
SCISSORS

A player chooses one of these constants, if both players choose the same constant then in fact they are pointing to the same object and the == works perfectly.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Simulate a game of Rock, Paper, Scissors