![]() !equals however I'm getting an error saying expected. I have made a simple Rock Scissors Paper game, however I want to implement validation so the user will be notified if they enter anything other than Rock Paper Scissors. Make the change on the string.intern () method. If present, it fetches the address ref from there and use it for comparison. Reverse of 'name equals A or B' - clearly a more sensible condition. If name does not equal A or does not equal B - it can't equal both at once, so is always true. When intern () method is called it checks the same value is present in the string constant pool. First condition will always return true as both the operands contradict each other. If ((Player1.equals("rock")) & (Player2.equals("scissors")))Įlse if ((Player1.equals("scissors")) & (Player2.equals("rock"))) Instead of equals () method use the intern () method on status string along with the. If ((Player1.equals("scissors")) & (Player2.equals("paper")))Įlse if ((Player1.equals("paper")) & (Player2.equals("scissors"))) See the code example and understand Java deals differently for lowercase and uppercase characters. That is why Java returns false to the console. We think that both the values are the same, but Java works on Unicode values, and both the variables hold different Unicode. ("Please insert either Rock Paper or Scissors!) Java does not consider lowercase and uppercase equal. On the other hand, equals () method compares whether the value of the strings is equal, and not. You should not use (equality operator) to compare these strings because they compare the reference of the string, i.e. If (Player1.!equals("rock" + "paper" + "scissors") To compare these strings in Java, we need to use the equals () method of the string. String Player2 = scan.next().toLowerCase() ("Player 2: Choose rock, paper or scissors: ") ![]() String Player1 = scan.next().toLowerCase() ("Player 1: Choose rock, paper or scissors: ")
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |