237,568 views
43 votes
43 votes
Could someone write the code for this in java? I cannot use java.util random(but i can use scanner). also, i cannot use switch or case.

Program Rock.java contains a skeleton for the game Rock, Paper, Scissors. 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:


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. Add a loop to make sure the user only enters an R, P, or S. The user's play is stored as a string to make it easy to convert whatever is entered to upper case. Use if statements to convert the randomly generated integer for the computer's play to a string.


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


// Add the code for each action beneath the comment. // more than one line may be needed in some cases // 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) – use (int) (Math.random() * 3) to


// generate a random play


// Translate computer's randomly generated play to string // using mutually exclusive conditional statements


// Print computer's play


// See who won. Use nested ifs instead of &&.


}


}


People hardly ever play Rock, Paper, Scissors one game at a time. They play matches that are best 2 out of 3, best 3 out of 5, etc. Change your program to prompt the user for the odd number of games they want to play and use a loop to make sure it is an odd number. Then, add a loop to your program to play the game multiple times. Keep track of who wins, and make sure the loop stops when either the computer or the user has accumulated enough wins. Ties do not count in either the game total or the win totals – so a best 2 out of 3 match requires 3 games to be played where someone wins 2 times.

User Shamas
by
2.4k points

1 Answer

11 votes
11 votes

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!

User Rapti
by
3.5k points