Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Random;
  3.  
  4. public class RPSGame
  5. {
  6.     public static void main(String[] args)
  7.     {
  8.         Scanner input = new Scanner(System.in);
  9.         Random random = new Random();
  10.  
  11.         int computerPickInt, rock = 0, paper = 1, scissors = 2;
  12.         String computerPick = "";
  13.         Boolean quit = false;
  14.  
  15.  
  16.         while(!quit) {
  17.             System.out.println("Enter move (rock, paper, or scissors):");
  18.             String userPick = input.nextLine();
  19.  
  20.             while (!userPick.equalsIgnoreCase("rock") && !userPick.equalsIgnoreCase("paper") && !userPick.equalsIgnoreCase("scissors")) {
  21.                 System.out.println("Illegal choice: " + userPick);
  22.                 System.out.print("Re-enter your play: ");
  23.                 userPick = input.nextLine();
  24.             }
  25.  
  26.             computerPickInt = random.nextInt(3);
  27.  
  28.             switch (computerPickInt) {
  29.                 case 0:
  30.                     computerPick = "rock";
  31.                     break;
  32.                 case 1:
  33.                     computerPick = "paper";
  34.                     break;
  35.                 case 2:
  36.                     computerPick = "scissors";
  37.             }
  38.  
  39.             System.out.println("Computer chooses " + computerPick);
  40.  
  41.             if (userPick.equalsIgnoreCase(computerPick))
  42.                 System.out.println("It's a tie!");
  43.             else if (userPick.equalsIgnoreCase("rock")) {
  44.                 if (computerPick.equals("paper"))
  45.                     System.out.println("Paper covers rock! You lose.");
  46.                 else
  47.                     System.out.println("Rock crushes scissors! You win!");
  48.             } else if (userPick.equalsIgnoreCase("paper")) {
  49.                 if (computerPick.equals("rock"))
  50.                     System.out.println("Paper covers rock! You win!");
  51.                 else
  52.                     System.out.println("Scissors cut paper! You lose.");
  53.             } else {
  54.                 if (computerPick.equals("rock"))
  55.                     System.out.println("Rock crushes scissors! You lose.");
  56.                 else
  57.                     System.out.println("Scissors cut paper! You win!");
  58.             }
  59.             System.out.print("Play again? Enter yes or no: ");
  60.             String playAgain = input.nextLine();
  61.  
  62.             while(!playAgain.equalsIgnoreCase("yes") && !playAgain.equalsIgnoreCase("no")){
  63.                 System.out.print("Sorry, that is invalid.\nEnter yes or no: ");
  64.                 playAgain = input.nextLine();
  65.             }
  66.             if(playAgain.equalsIgnoreCase("no"))
  67.                 quit = true;
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement