0% found this document useful (0 votes)
13 views2 pages

Rock Paper Scissors

This Java program allows a user to play rock paper scissors against a computer opponent by randomly generating the computer's choice, prompting the user for their choice, determining the winner, and allowing the user to play again if desired.

Uploaded by

Eunice Alonzo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Rock Paper Scissors

This Java program allows a user to play rock paper scissors against a computer opponent by randomly generating the computer's choice, prompting the user for their choice, determining the winner, and allowing the user to play again if desired.

Uploaded by

Eunice Alonzo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

Random;
import java.util.Scanner;

public class Main {

public static int arr[] = {1,2,3};


public static Random rand = new Random();
public static Scanner inp = new Scanner(System.in);

public static int play() {


int oppchoice = rand.nextInt(3)+1;
return oppchoice;
}
public static void ask() {
System.out.println("1 = Rock, 2 = Paper, 3 = Scissors. Go pick!");
int choice = inp.nextInt();
switch (choice) {
case 1:
System.out.println("You picked rock.");
if (play() == 1) {
System.out.println("They picked rock..");
System.out.println("Tie");
}
else if (play() == 2) {
System.out.println("They picked paper..");
System.out.println("You win!");
}
else {
System.out.println("They picked scissors..");
System.out.println("You lose..");
}
break;
case 2:
System.out.println("You picked paper.");
if (play() == 1) {
System.out.println("They picked rock..");
System.out.println("You win!");
}
else if (play() == 2) {
System.out.println("They picked paper..");
System.out.println("Tie");
}
else {
System.out.println("They picked scissors..");
System.out.println("You lose..");
}
break;
case 3:
System.out.println("You picked scissors.");
if (play() == 1) {
System.out.println("They picked rock..");
System.out.println("You lose..");
}
else if (play() == 2) {
System.out.println("They picked paper..");
System.out.println("You win!");
}
else {
System.out.println("They picked scissors..");
System.out.println("Tie");
}
break;
}
}

public static int playAgain() {

System.out.println("Want to play again? 1 if yes, 0 if no");


int choice = inp.nextInt();
switch(choice) {
case 1:
play();
ask();
break;
case 0:
System.out.println("Oki bye");
}

return choice;
}

public static void main (String[]args) {


play();
ask();
playAgain();
while (playAgain() == 1) {
playAgain();
}

}
}

You might also like