This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
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

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Simulate a game of Rock, Paper, Scissors" Watch "Simulate a game of Rock, Paper, Scissors" New topic

Simulate a game of Rock, Paper, Scissors

Todd Smith Todd

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

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.

[ 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

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.
I agree. Here's the link:
subject: Simulate a game of Rock, Paper, Scissors
It's not a secret anymore!