Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>How about using an <code>enum</code> here's the entire game...</p> <pre><code>public static enum RPS { ROCK, PAPER, SCISSORS; // Simple method to get the appropriate enum. public static RPS fromString(String in) { if (in.toLowerCase().startsWith("r")) { return ROCK; } else if (in.toLowerCase().startsWith("p")) { return PAPER; } else if (in.toLowerCase().startsWith("s")) { return SCISSORS; } return null; } // Calculate the winner in a contest. public Boolean wins(RPS in) { if (this == in) { // Tie! return null; } switch (this) { case ROCK: if (in == SCISSORS) { // Rock beats scissors. return true; } return false; case PAPER: if (in == ROCK) { // Paper beats rock. return true; } return false; case SCISSORS: if (in == PAPER) { // Scissors beats paper. return true; } return false; } return null; } } public static void main(String[] args) { Random r = new Random(System.currentTimeMillis()); String[] choices = new String[] { "R", "P", "S" }; Scanner in = new Scanner(System.in); while (in.hasNextLine()) { System.out.println("Please enter (R)ock, (P)aper " + "or (S)cissors to play. (Q)uit."); String c = in.nextLine(); c = c.trim(); if (c.toLowerCase().startsWith("q")) { break; } RPS player = RPS.fromString(c); RPS computer = RPS.fromString(choices[r .nextInt(choices.length)]); System.out.println("Computer picked " + computer); if (player.wins(computer) == null) { System.out.println("It's a Tie"); } else if (player.wins(computer)) { System.out.println("You won"); } else { System.out.println("The comptuer won"); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload