0

I've run into a few problems with this one. I've tried my best following the directions. I can't figure out how to properly use "switch" since we haven't really put it to use in class. I want to create random integers (this I know how to do) that will become Strings (letters converted to strings). How would I go about converting the integers to "strings" from the alphabet?

Also, if I comment out the switch statement I get a compile error about my else and if states for the game.

Help with any details you can.

Thanks!

/*Rock, Paper, Scissors

$ java Rock
Enter your play: R, P, or S
r
Computer play is S
Rock crushes scissors, you win!

Note that the user should be able to enter either upper or lower case r, p, and s. The user's play is stored as a 
string to make it easy to convert whatever is entered to upper case. Use a switch statement to convert the randomly 
generated integer for the computer's play to a string.*/

// ****************************************************************
//   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();

	//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
	switch (computerInt)
{

	}

	//Print computer's play
	//See who won.
	if (personPlay.equals(computerPlay))  
	    System.out.println("It's a tie!");
		 
	else if (personPlay.equals("R"))
		{
	    if (computerPlay.equals("S"))
		System.out.println("Rock crushes scissors.  You win!!");
	    else (computerPlay.equals("P"))
		System.out.println ("Paper eats rock. You lose!!");
		}
	else if (personPlay.equals("P"))
		{
	    if (computerPlay.equals("S"))
		System.out.println("Scissor cuts paper.  You lose!!");
	    else (personPlay.equals("R"))
		System.out.println ("Paper eats rock. You win!!");
		}
	else if (personPlay.equals("S"))
		{
		 if (computerPlay.equals("P"))
		System.out.println ("Scissor cuts paper. You win!!");
		 else (computerPlay.equals("R"))
		System.out.println ("Rock breaks scissors. You lose!!");
		}
    }
}
1
Contributor
1
Reply
9
Views
7 Years
Discussion Span
Last Post by EGutierrez91
0

Solved. In case anyone ever needs it its here...

/*Rock, Paper, Scissors

Program Rock.java contains a skeleton for the game Rock, Paper, Scissors. Open it and save it to your directory. 
Add statements to the program as indicated by the comments so that the program asks the user to enter a play, 
generates a random play for the computer, compares them and announces the winner (and why). For example, one run 
of your program might look like this: 

$ java Rock
Enter your play: R, P, or S
r
Computer play is S
Rock crushes scissors, you win!

Note that the user should be able to enter either upper or lower case r, p, and s. The user's play is stored as a 
string to make it easy to convert whatever is entered to upper case. Use a switch statement to convert the randomly 
generated integer for the computer's play to a string.*/

// ****************************************************************
//   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 ("Enter R for Rock, P for Paper, S for Scissors: "); //Get player's play -- note that this is stored as a string
	personPlay = scan.next();
	
	personPlay = personPlay.toUpperCase();
	
	computerInt = generator.nextInt(3);
	
	switch (computerInt)
	{
		case 0:
			{
			computerPlay = "R";
			break;
			}
		case 1:
			{
			computerPlay = "P";
			break;
			}
		case 2:
			{
			computerPlay = "S";
			break;
			}
		default:
			{
			computerPlay = "will not happen";
			}	
	}
	
	System.out.println ("Computer plays: " + computerPlay);
	
		if (personPlay.equals(computerPlay))
		{ 
			System.out.println("It's a tie!");
		}
		 
		else 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 eats rock. You lose!!");
		}
		else if (personPlay.equals("P"))
		{
	   	if (computerPlay.equals("S"))
			System.out.println ("Scissor cuts paper. You lose!!");
	   	else if (computerPlay.equals("R"))
			System.out.println ("Paper eats rock. You win!!");
		}
		else if (personPlay.equals("S"))
		{
			if (computerPlay.equals("P"))
			System.out.println ("Scissor cuts paper. You win!!");
			else if (computerPlay.equals("R"))
			System.out.println ("Rock breaks scissors. You lose!!");
		}
		else
		{
			System.out.println ("Invalid user input.");
		}
		
		
	}
}

// Enter R for Rock, P for Paper, S for Scissors: 
// P
// Computer plays: P
// It's a tie!
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.