-3
public static String winner(String computer,String user)
{
    String win=" ";
    if (computer=="rock" && user=="scissors")
    {
        win="Computer wins! Rock smashes scissors.";
    }
    else if (computer=="paper" && user=="rock")
    {
        win="Computer wins! Paper wraps rock.";
    }
    else if (computer=="scissors" && user=="paper")
    {
        win="Computer wins! Scissors cuts paper.";
    }
    else if (computer==user)
    {
        win="Tie. No winner";
    }
    else if (user=="rock" && computer=="scissors")
    {
        win="YOU win! Rock smashes scissors.";
    }
    else if (user=="paper" && computer=="rock")
    {
        win="YOU win! Paper wraps rock.";
    }
    else if (user=="scissors" && computer=="paper")
    {
        win="YOU win! Scissors cuts paper.";
    }
    return win;
}

The method receives 2 lower case strings as parameters. I have checked by printing the parameters. I do not understand why my if else statements are not working. I am simply trying to do the possibles out comes of a rock paper scissors game. Thanks for all your help guys.

Chevyb
  • 55
  • 3

1 Answers1

2

You should compare strings with equals in Java, not ==, or you will be comparing references, not value.

How do I compare strings in Java?

Community
  • 1
  • 1
Hugo Sousa
  • 1,904
  • 2
  • 15
  • 28