Skip to content

Instantly share code, notes, and snippets.

@inDream
Created October 11, 2017 04:48
Show Gist options
  • Save inDream/4e6ce34b9cd5a0b6172c8541d5e9e0e0 to your computer and use it in GitHub Desktop.
Save inDream/4e6ce34b9cd5a0b6172c8541d5e9e0e0 to your computer and use it in GitHub Desktop.
import java.util.*;
public class RockPaperScissors {
public static void main(String[]args) {
int s[] = {2, 0, 1};
String name[] = {"rock", "paper", "scissors"};
System.out.println("Rock-Paper-Scissors\n===================");
Scanner scn = new Scanner(System.in);
System.out.print("Enter 0 for rock, 1 for paper, or 2 for scissors: ");
int A = scn.nextInt();
Random ran = new Random();
int B = ran.nextInt(3);
if (A < 0 || A >= 3) {
System.out.println("Invalid input!");
return;
}
System.out.println("Player picks " + name[A] + ".\nComputer picks " + name[B] + ".");
if (A == B) {
System.out.println("Draw game!");
} else {
System.out.println((s[A] == B ? "Player" : "Computer") + " wins!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment