Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Don't compare Strings using <code>==</code>. Use the equals or the equalsIgnoreCase(...) method instead. Understand that == checks if the two <em>objects</em> are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. So instead of</p> <pre><code>if (fu == "bar") { // do something } </code></pre> <p>do, </p> <pre><code>if ("bar".equals(fu)) { // do something } </code></pre> <p>or,</p> <pre><code>if ("bar".equalsIgnoreCase(fu)) { // do something } </code></pre> <p>One way to simplify your code is to create a RockPaperScissors enum and give it its on compare method. Something like:</p> <pre><code>enum RockPaperScissors { ROCK("Rock"), PAPER("Paper"), SCISSORS("Scissors"); private String text; private static int[][] winMatrix = {{0, -1, 1}, {1, 0, -1}, {-1, 1, 0}}; private RockPaperScissors(String text) { this.text = text; } @Override public String toString() { return text; } // can't use compareTo since it is a final method for enums public int compareVs(RockPaperScissors other) { int thisOrdinal = ordinal(); int otherOrdinal = other.ordinal(); return winMatrix[thisOrdinal][otherOrdinal]; } } </code></pre> <p>Then to compare one enum vs. another, simply call its compareVs(...) method passing in the other enum.</p> <p>So your huge if/else block would reduce down to:</p> <pre><code>// assuming that user and comp are RockPaperScissors variables int result = user.compareVs(comp); if (result == 1) { System.out.println("You've won!"); } else if (result == 0) { System.out.println("It's a tie!"); } if (result == -1) { System.out.println("You've lost!"); } </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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