Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Random;
- /*Program Name: RockPaperScissors.java
- Author: Yumi Lamansky
- Class: CSC110 CRN 37211
- Date Written: 09/18/2018
- Brief Description: Simulates rock paper scissors game;
- user against the computer
- Description of inputs: Rock, Paper, or Scissors
- Description of outputs: Users choice, computers choice,
- Winner
- */
- public class RockPaperScissors
- {
- public static void main(String[] args)
- {
- Random random = new Random();
- Scanner keyboard = new Scanner(System.in);
- String user;
- String winner;
- System.out.println("*****ROCK PAPER SCISSORS GAME*****");
- //Computers choice
- int num;
- num = random.nextInt(3) + 1;
- String comp = "";
- if (num == 1)
- {
- comp = "Rock";
- }
- else if (num == 2)
- {
- comp = "Paper";
- }
- else if (num == 3)
- {
- comp = "Scissors";
- }
- //user picks
- System.out.println("Enter one of the following: " +
- "\n*Rock\n*Paper\n*Scissors");
- user = keyboard.nextLine();
- //invalid input
- boolean invalid = false;
- //when winning
- String win = "";
- //ROCK
- if (comp.equals("Rock") && user.equalsIgnoreCase("Scissors"))
- {
- win = "Computer";
- }
- else if (user.equalsIgnoreCase("Rock") && comp.equals("Scissors"))
- {
- win = "You!";
- }
- //PAPER
- else if (comp.equals("Paper") && user.equalsIgnoreCase("Rock"))
- {
- win = "Computer";
- }
- else if (user.equalsIgnoreCase("Paper") && comp.equals("Rock"))
- {
- win = "You!";
- }
- //SCISSORS
- else if (comp.equals("Scissors") && user.equalsIgnoreCase("Paper"))
- {
- win = "Computer";
- }
- else if (user.equalsIgnoreCase("Scissors") && comp.equals("Paper"))
- {
- win = "You!";
- }
- //TIE
- else if (user.equalsIgnoreCase(comp))
- {
- win = "Tie";
- }
- else
- {
- System.out.println("INVALID INPUT");
- invalid = true;
- }
- //Display outputs
- if (!invalid)
- {
- System.out.println("You picked: " + user);
- System.out.println("Computer picked: " + comp);
- System.out.println("Winner: " + win);
- }
- }
- }
- /*Expected output:
- *****ROCK PAPER SCISSORS GAME*****
- Enter one of the following:
- *Rock
- *Paper
- *Scissors
- [ input ]
- You picked: [users input]
- Computer picked: [random]
- Winner: [winner]
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement