Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Scanner;
- public class Lab9 {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- // ask user to enter a number
- System.out.println("Welcome! Enter a number to select scissors, rock or paper.\n" +
- "scissors (0), rock (1), paper (2): ");
- int choice = input.nextInt();
- // generate random number
- Random randomNumber = new Random();
- int computerChoice = randomNumber.nextInt((2) + 1);
- System.out.println(computerChoice);
- switch (choice) {
- case 0:
- switch (computerChoice) {
- case 0:
- System.out.println("You both chose scissors, it's a tie!");
- break;
- case 1:
- System.out.println("The computer is rock, you are scissors - you lose!");
- break;
- case 2:
- System.out.println("The computer is paper, you are scissors - you win!");
- }
- break;
- case 1:
- switch (computerChoice) {
- case 0:
- System.out.println("The computer is scissors, you are rock - you win!");
- break;
- case 1:
- System.out.println("You both chose rock, it's a tie!");
- break;
- case 2:
- System.out.println("The computer is paper, you are rock - you lose!");
- }
- break;
- case 2:
- switch (computerChoice) {
- case 0:
- System.out.println("The computer is scissors, you are paper - you win!");
- break;
- case 1:
- System.out.println("The computer is rock, you are paper - you win!");
- break;
- case 2:
- System.out.println("You both chose paper, it's a tie!");
- }
- break;
- default:
- System.out.println("Enter a valid number");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement