Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Random;
- public class RPSGame
- {
- public static void main(String[] args)
- {
- Scanner input = new Scanner(System.in);
- Random random = new Random();
- int computerPickInt, rock = 0, paper = 1, scissors = 2;
- String computerPick = "";
- Boolean quit = false;
- while(!quit) {
- System.out.println("Enter move (rock, paper, or scissors):");
- String userPick = input.nextLine();
- while (!userPick.equalsIgnoreCase("rock") && !userPick.equalsIgnoreCase("paper") && !userPick.equalsIgnoreCase("scissors")) {
- System.out.println("Illegal choice: " + userPick);
- System.out.print("Re-enter your play: ");
- userPick = input.nextLine();
- }
- computerPickInt = random.nextInt(3);
- switch (computerPickInt) {
- case 0:
- computerPick = "rock";
- break;
- case 1:
- computerPick = "paper";
- break;
- case 2:
- computerPick = "scissors";
- }
- System.out.println("Computer chooses " + computerPick);
- if (userPick.equalsIgnoreCase(computerPick))
- System.out.println("It's a tie!");
- else if (userPick.equalsIgnoreCase("rock")) {
- if (computerPick.equals("paper"))
- System.out.println("Paper covers rock! You lose.");
- else
- System.out.println("Rock crushes scissors! You win!");
- } else if (userPick.equalsIgnoreCase("paper")) {
- if (computerPick.equals("rock"))
- System.out.println("Paper covers rock! You win!");
- else
- System.out.println("Scissors cut paper! You lose.");
- } else {
- if (computerPick.equals("rock"))
- System.out.println("Rock crushes scissors! You lose.");
- else
- System.out.println("Scissors cut paper! You win!");
- }
- System.out.print("Play again? Enter yes or no: ");
- String playAgain = input.nextLine();
- while(!playAgain.equalsIgnoreCase("yes") && !playAgain.equalsIgnoreCase("no")){
- System.out.print("Sorry, that is invalid.\nEnter yes or no: ");
- playAgain = input.nextLine();
- }
- if(playAgain.equalsIgnoreCase("no"))
- quit = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement