Skip to content

Instantly share code, notes, and snippets.

@maxandersen
Last active October 21, 2022 13:40
Show Gist options
  • Save maxandersen/785d6c8940cc29f66c9f46e97f387b75 to your computer and use it in GitHub Desktop.
Save maxandersen/785d6c8940cc29f66c9f46e97f387b75 to your computer and use it in GitHub Desktop.
import static java.lang.System.*;
import java.util.*;
out.println("Let's play Rock Paper Scissors!");
var r = "rock";
var p = "paper";
var s = "scissors";
var all_choices = List.of(r, p, s);
var user = console().readLine("Enter a choice (%s, %s, %s): ", r, p, s);
if (!all_choices.contains(user)) {
out.println("Invalid choice! ");
exit(1);
}
var computer = all_choices.get((int) (Math.random()*3));
out.printf("You chose %s, computer chose %s.%n", user, computer);
// r>s, p>r, s>p
if (user.equals(computer)) {
out.println("Tie!");
} else if (user.equals(r) && computer.equals(s) ||
user.equals(p) && computer.equals(r) ||
user.equals(s) && computer.equals(p)) {
out.println("You win!");
} else {
out.println("You lose!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment