0

Trying to create a rock paper scissor game but after getting the users choice then randomly generating the computers choice and displaying it in the program. The program doesn't go onto the next set of code which determines the winner.

    Scanner in = new Scanner(System.in);

    int ComputerChoice = (int)(3 * Math.random()) + 1;

    System.out.println("Please select one of the following [R/P/S]: ");
    String UserChoice = in.nextLine();

    if (UserChoice.equalsIgnoreCase("R") ) {
        System.out.println("You chose: Rock"); } 

    else if (UserChoice.equalsIgnoreCase("P") ) {
        System.out.println("You chose: Paper"); } 

    else if (UserChoice.equalsIgnoreCase("S") ) {
        System.out.println("You chose: Scissors"); } 

    else if (UserChoice != "R" || UserChoice != "S" || UserChoice != "P") {
            UserChoice = "R";
            System.out.println("Invalid choice! Defaulting to Rock.");
        }

    if (ComputerChoice == 1) {
        System.out.println("I chose: Rock");
    }
    else if (ComputerChoice == 2) {
        System.out.println("I chose: Paper");
    }
    else if (ComputerChoice == 3) {
        System.out.println("I chose: Scissors");
        }

    if (UserChoice == "R" && ComputerChoice == 1) {
        System.out.println("A Tie!");
    }
    else if (UserChoice == "R" && ComputerChoice == 2) {
        System.out.println("Paper beats rock - you lose!");
    }
    else if (UserChoice == "R" && ComputerChoice == 3) {
        System.out.println("Rock beats scissors - you win!");
    }
    else if (UserChoice == "P" && ComputerChoice == 2) {
        System.out.println("A Tie!");
    }
    else if (UserChoice == "P" && ComputerChoice == 1) {
        System.out.println("Paper beats rock - you win!");
    }
    else if (UserChoice == "P" && ComputerChoice == 3) {
        System.out.println("Scissors beats paper - you lose!");
    }
    else if (UserChoice == "S" && ComputerChoice == 1) {
        System.out.println("Rock beats scissors - you lose!");
    }
    else if (UserChoice == "S" && ComputerChoice == 3) {
        System.out.println("A Tie!");
    }
    else if (UserChoice == "s" && ComputerChoice == 2) {
        System.out.println("Scissors beats paper - you win!");
  • `== "R"` strikes again, but based on `equalsIgnoreCase` in your question you seem to know [how we should compare strings in Java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). – Pshemo Feb 12 '15 at 19:06
  • 1
    Don't compare Strings using `==` or `!=`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two *objects* 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. – Hovercraft Full Of Eels Feb 12 '15 at 19:07
  • Wow thanks! That helped me a lot and it works the way my professors want it! – Hunter Gibson Feb 12 '15 at 19:12

0 Answers0