3 Replies - 387 Views - Last Post: 05 June 2014 - 06:49 PM Rate Topic: -----

#1 blvckphvroh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-May 14

Rock, Paper, Scissors Program

Posted 05 June 2014 - 06:22 PM

I'm running into trouble with the beginning part of this rock, paper, scissors, program. Can't quite figure out why it's only displaying the computer's move and not the user's

import java.util.Scanner;

public class Project04 {

	public static void main(String[] args) {
		
		Scanner keyboard = new Scanner(System.in);
		
		int computer =  (int)(3 * Math.random()) + 1;
		String computerMove = "";
		
		if(computer == 1){
			computerMove = "Rock";
		}
		else if (computer == 2){
			computerMove = "Paper";
		}
		else if (computer == 3){
			computerMove = "Scissors";
		}	
		
		System.out.print("Please select one of [R/P/S]: ");
		String userplay = keyboard.nextLine();
		String userMove = "";
		
		if((userplay == "R") || (userplay == "r")){
			  userMove = "Rock";
		}
		else if (userplay == "P" || userplay == "p"){
				userMove = "Paper";
		}
		else if (userplay == "S" || userplay == "s"){
				userMove = "Scissors";
		}
		
		System.out.println("You chose: "+ userMove);
		System.out.println("I chose: " + computerMove);
		
		
		
	}

}




Output:

Please select one of [R/P/S]: r
You chose:
I chose: Paper


*** It should be displaying Rock as the user's choice, however it isn't returning anything.

Is This A Good Question/Topic? 0
  • +

Replies To: Rock, Paper, Scissors Program

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7769
  • View blog
  • Posts: 13,137
  • Joined: 19-March 11

Re: Rock, Paper, Scissors Program

Posted 05 June 2014 - 06:37 PM

Use .equals to compare Strings. The == comparison compares reference identity, not value equality.
Was This Post Helpful? 0
  • +
  • -

#3 blvckphvroh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-May 14

Re: Rock, Paper, Scissors Program

Posted 05 June 2014 - 06:44 PM

Thank you!!!! It works now....can't believe I didn't catch that lol



View Postjon.kiparsky, on 05 June 2014 - 06:37 PM, said:

Use .equals to compare Strings. The == comparison compares reference identity, not value equality.

Was This Post Helpful? 0
  • +
  • -

#4 oha055  Icon User is online

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 253
  • Joined: 02-February 09

Re: Rock, Paper, Scissors Program

Posted 05 June 2014 - 06:49 PM

Also, instead of (userplay == "P" || userplay == "p"), you could do userplay.toLowerCase().equals("p"), so that you wont have to explicitly check for upper and lower case equality.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1