Skip to content

Instantly share code, notes, and snippets.

@deepakkhattar26o2
Last active January 11, 2022 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deepakkhattar26o2/47774b45f0bb5b4d9178d9e9781d60a7 to your computer and use it in GitHub Desktop.
Save deepakkhattar26o2/47774b45f0bb5b4d9178d9e9781d60a7 to your computer and use it in GitHub Desktop.
RockPaperScissors with Java
import java.util.Random;
import java.util.Scanner;
public class rockPaperScissor {
public static void compChoice(int computerInput){
if (computerInput == 0) {
System.out.println("Computer choice: Rock");
} else if (computerInput == 1) {
System.out.println("Computer choice: Paper");
} else {
System.out.println("Computer choice: Scissors");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean matchOver = false;
while(!matchOver)
{
System.out.print("Enter 0 for Rock, 1 for Paper, 2 for Scissor: ");
int userInput = sc.nextInt();
Random random = new Random();
int computerInput = random.nextInt(3);
if (userInput == computerInput) {
compChoice(computerInput);
System.out.println("Draw");
} else if (userInput == 0 && computerInput == 2 || userInput == 1 && computerInput == 0
|| userInput == 2 && computerInput == 1) {
compChoice(computerInput);
System.out.println("You Win!");
matchOver = true;
} else {
compChoice(computerInput);
System.out.println("You Lose!");
matchOver = true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment