Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ****************************************************************
- // Rock.java
- //
- // Play Rock, Paper, Scissors with the user
- //
- // ****************************************************************
- import java.util.Scanner;
- import java.util.Random;
- public class rockpaperscissors
- {
- 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();
- //where a random number gets generated
- computerInt=generator.nextInt(3);
- System.out.println(""+computerInt);
- //Get player's play -- note that this is stored as a string
- System.out.println(("Enter R P S "));
- personPlay=scan.next();
- //Make player's play uppercase for ease of comparison //Generate computer's play (0,1,2)
- personPlay= personPlay.toUpperCase();
- System.out.println("You chose: " +personPlay);
- //Translate computer's randomly generated play to string
- switch (computerInt){
- case 0:computerPlay = "R"; break;
- case 1:computerPlay = "P"; break;
- case 2:computerPlay = "S"; break;
- default:computerPlay= " "; break;
- }
- //Print computer's play
- System.out.println("The computer chose: "+computerPlay);
- {
- //See who won. Use nested ifs instead of &&
- 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 { System.out.println("Rock crushes scissors. You loose!!");}}
- if (personPlay.equals("P")){
- if (computerPlay.equals("R")){
- System.out.println("Paper covers rock. You win!!");}
- else {
- System.out.println("Paper covers rock. You loose!!");}}
- if (personPlay.equals("S")){
- if (computerPlay.equals("P")){
- System.out.println("Scissors cuts paper. You win!!");}
- else{ System.out.println("Scissors cuts paper You loose!!");}}
- }
- }
- }
Add Comment
Please, Sign In to add comment