14 Replies - 2736 Views - Last Post: 18 October 2011 - 06:41 PM Rate Topic: -----

#1 juadams  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-October 11

How to fix my Rock, Paper, Scissors code?

Posted 18 October 2011 - 01:09 PM

The part i need help with is past the "//Determining the winner"

// ****************************************************************
//   Rock.java
//
//   Play Rock, Paper, Scissors with the user
//          
// ****************************************************************
import java.util.Scanner;
import java.util.Random;

public class Rock
{
    public static void main(String[] args)
    {
	String personPlay;    //User's play -- "R", "P", or "S"
	String computerPlay;  //Computer's play -- "R", "P", or "S"
	int computerInt;      //Randomly generated number used to determine
	                      //computer's play

      Scanner scan = new Scanner(System.in);
	Random generator = new Random();
	System.out.println("Please  enter you play R, P, or S!");
	personPlay = scan.nextLine();
			

	//Get player's play -- note that this is stored as a string
	//Make player's play uppercase for ease of comparison
	//Generate computer's play (0,1,2)
	//Translate computer's randomly generated play to string
	
    personPlay = personPlay.toUpperCase();
	
	computerInt = generator.nextInt(3);
	
	switch (computerInt){
    case 0:
		computerPlay = "R";
		System.out.println ("Computer has chosen ROCK.");
		break;
	case 1:
		computerPlay = "P";
		System.out.println ("Computer has chosen PAPER.");
		break;
	case 2:
		computerPlay = "S";
		System.out.println ("Computer has chosen SCISSORS.");
		break;
	
	}
	
//Determine who wins                                                     
if (personPlay.equals(computerPlay));
   System.out.println ("It's a tie!");
     if (personPlay.equals("R"))
     if (computerPlay.equals("S"))
     System.out.println ("Rock crushes scissors. You win!");
       else if (computerPlay.equals("P"));
       System.out.println ("Paper beats rock. Computer wins!");
   if (personPlay.equals("P"))
   if (computerPlay.equals("R"))
    System.out.println ("Paper beats rock. You win!");
        else if (computerPlay.equals("S"))
        System.out.println ("Scissors cut paper. Computer wins!");
else if (personPlay.equals("S"))
     if (computerPlay.equals("R"))
     System.out.println ("Rock crushes scissors. Computer wins!");
else if (computerPlay.equals("P"))
     System.out.println ("Scissors cut paper. You win!");
    } 
}


This post has been edited by macosxnerd101: 18 October 2011 - 01:11 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: How to fix my Rock, Paper, Scissors code?

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: How to fix my Rock, Paper, Scissors code?

Posted 18 October 2011 - 01:11 PM

What specific problems or errors are you encountering?
Was This Post Helpful? 0
  • +
  • -

#3 juadams  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-October 11

Re: How to fix my Rock, Paper, Scissors code?

Posted 18 October 2011 - 01:16 PM

It isnt recognizing computerPlay
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: How to fix my Rock, Paper, Scissors code?

Posted 18 October 2011 - 01:23 PM

Your problem is the semicolon at the if statement:
if (personPlay.equals(computerPlay));
that causes the next statement to be executed whatever happens
Was This Post Helpful? 1
  • +
  • -

#5 juadams  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-October 11

Re: How to fix my Rock, Paper, Scissors code?

Posted 18 October 2011 - 01:24 PM

Im not that good with The If/ else statements.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: How to fix my Rock, Paper, Scissors code?

Posted 18 October 2011 - 01:26 PM

This:
if (personPlay.equals(computerPlay));
      System.out.println("Test");



Is the same as:
System.out.println("Test");



Because the if statement does not encapsulate the println() statement due to the semi-colon. Remove that end semi-colon, as smohd suggested, and the next statement will be encapsulated in the if statement.
Was This Post Helpful? 0
  • +
  • -

#7 juadams  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-October 11

Re: How to fix my Rock, Paper, Scissors code?

Posted 18 October 2011 - 01:26 PM

The computer play has a red line under it.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: How to fix my Rock, Paper, Scissors code?

Posted 18 October 2011 - 01:28 PM

Then post the error associated with the red line, as well as the exact line where the error occurs.
Was This Post Helpful? 0
  • +
  • -

#9 juadams  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-October 11

Re: How to fix my Rock, Paper, Scissors code?

Posted 18 October 2011 - 01:31 PM

The local variable computerPlay may not have been initialized

Lines 51 54 56 59 61 64 66
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: How to fix my Rock, Paper, Scissors code?

Posted 18 October 2011 - 01:32 PM

Because computerPlay only receives assignments in conditional blocks, it may never be initialized. So you should initialize it, or assign it an initial value, when you declare it.
Was This Post Helpful? 1
  • +
  • -

#11 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: How to fix my Rock, Paper, Scissors code?

Posted 18 October 2011 - 01:34 PM

The computer doesn't know that your switch statement covers all the possibilities for your computerInt, so there is a chance that the variable will never have been intialized when you call it. At the top you can initialize it to "" or "none". Then if you ever change your random number generator to 0-3, the switch statement won't catch it, but there will still b e an output.
Was This Post Helpful? 1
  • +
  • -

#12 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: How to fix my Rock, Paper, Scissors code?

Posted 18 October 2011 - 01:34 PM

If you are not good with if/else I will advice you use braces {} in each of your cases. This will help you to understand the logic of execution and the blocks in it. So for example in your case you have four cases that when one happens other should not(and each one has other cases in it). So it will be something like:
if (personPlay.equals(computerPlay)){ //first case
   System.out.println ("It's a tie!"); //output of the first case
}//end of one case
else if (personPlay.equals("R")){ //second case
    //nested ifs follow here for second case
} //end of second case
else if (personPlay.equals("S")){ //third case
     //code here
}//end of third case
else if (personPlay.equals("P")){ //fourth case
     //code here computer wins
}//end of fouth case

or something like that...
Was This Post Helpful? 1
  • +
  • -

#13 juadams  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 18-October 11

Re: How to fix my Rock, Paper, Scissors code?

Posted 18 October 2011 - 02:23 PM

I changed the code but yet it wont run the if else statements now.

// ****************************************************************
//   Rock.java
//
//   Play Rock, Paper, Scissors with the user
//          
// ****************************************************************
import java.util.Scanner;
import java.util.Random;

public class Rock {
	public static void main(String[] args) {
		String personPlay; // User's play -- "R", "P", or "S"
		String computerPlay = null; // Computer's play -- "R", "P", or "S"
		int computerInt; // Randomly generated number used to determine
							// computer's play

		Scanner scan = new Scanner(System.in);
		Random generator = new Random();
		System.out.println("Please  enter you play R, P, or S!");
		personPlay = scan.nextLine();

		// Get player's play -- note that this is stored as a string
		// Make player's play uppercase for ease of comparison
		// Generate computer's play (0,1,2)
		// Translate computer's randomly generated play to string

		personPlay = personPlay.toUpperCase();

		computerInt = generator.nextInt(2);

		switch (computerInt) {
		case 0:
			computerPlay = "Rock";
			break;
		case 1:
			computerPlay = "Paper";
			break;
		case 2:
			computerPlay = "Scissors";
			break;

		}

		System.out.println("The computer chose: " + computerPlay);
		

		if (personPlay.equals(computerPlay))
			System.out.println("It's a tie!");
		else if (personPlay.equals("Rock"))
			if (computerPlay.equals("Scissors"))
				System.out.println("Rock crushes scissors.  You win!!");
			else if (computerPlay.equals("Paper"))
				System.out.println("Paper beats rock. Computer wins!");
			else if (personPlay.equals("Paper"))
				if (computerPlay.equals("Rock"))
					System.out.println("Paper beats rock. You win!");
				else if (computerPlay.equals("Scissors"))
					System.out.println("Scissors cut paper. Computer wins!");
				else if (personPlay.equals("Scissors"))
					if (computerPlay.equals("Rock"))
						System.out
								.println("Rock crushes scissors. Computer wins!");
					else if (computerPlay.equals("Paper"))
						System.out.println("Scissors cut paper. You win!");

	}

}


This post has been edited by macosxnerd101: 18 October 2011 - 02:24 PM
Reason for edit:: Please use code tags: [code] Code Goes Here [/code]

Was This Post Helpful? 0
  • +
  • -

#14 ChrisKellyDev  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 05-October 11

Re: How to fix my Rock, Paper, Scissors code?

Posted 18 October 2011 - 06:33 PM

I haven't played with random in a while but... another issue you may have is:

Quote

"nextInt", it will return an integer from a uniform distribution between 0 and one less than the parameter.


So by using random.nextInt(2) you're only capable of rolling 0 or 1.

From here:
http://www.cs.genese...nce/random.html

For future projects you could just use modulo operations:

int computerPlay = generator.nextInt()%3



Another issue you have is that you tell the user to input P, R, or S so if a user actually inputs a single character, you will never get a #t out of the if statements.

personPlay.equals(computerPlay)


That will be interpreted as "does Paper = P?" which is false. Also, if you're inputting say "Paper" you will also get false as it will try "does Paper = PAPER" because of the .toUpperCase().
Was This Post Helpful? 1
  • +
  • -

#15 ChrisKellyDev  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 05-October 11

Re: How to fix my Rock, Paper, Scissors code?

Posted 18 October 2011 - 06:41 PM

Oh, and you can simplify down those if/else statements.


if (computerPlay.equals(personPlay))
//It's a tie
;
else if (personPlay.equals("R") && computerPlay.equals("Scissors"))
//Player Wins
;
else if (personPlay.equals("P") && computerPlay.equals("Rock"))
//Player Wins
;
else if (personPlay.equals("S") && computerPlay.equals("Paper"))
//Player Wins
;
else
//Computer Wins
;



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1