1

So I made this rock paper scissors game a while ago using Java and I found a little bug I forgot to fix when I showed it to my friend today. Basically, the code takes a user input (i.e rock, paper, or scissors) and if their input does not equal rock, paper, or scissors, the main while loop is broken and the game is stopped.

How can I make it so that not only does it break the loop, but restarts it as well? I want to do this so that when someone gives an invalid input it automatically restarts so the player doesn't need to run the program all over again.

Here's the code for my main class:

public class rps {
    public static void main(String []args){
        boolean gameRunning = true;
        while(gameRunning) {
            System.out.println("Do you choose rock, paper, or scissors?");
            UserChoice userInput = new UserChoice();
            String userChoice = userInput.userDecide();
            if(userChoice != "rock" || userChoice != "paper" || userChoice != "scissors") {
                System.out.println("'" + userChoice + "'" + " is not a valid choice.");
                System.out.println("Please choose between rock, paper, or scissors.");
                gameRunning = false;
                break;
                // this is where I want to restart the function 
            }
            System.out.println("You threw: " + userChoice);
            ComputerInput computerChoice = new ComputerInput();
            String computerInput = computerChoice.computerDecide();
            System.out.println("Computer threw: " + computerInput);
            CompareChoices compareResults = new CompareChoices();
            gameRunning = compareResults.compare(userChoice, computerInput);
        }
    }
};

UPDATE

I figured out a few of the problems thanks to some help from the nice people on here. I used && instead of || (which is dumb because I originally used && anyways -_-), I used the "continue" statement instead of break, I removed gameRunning = false;, and I changed the way userChoice was compared to the valid responses.

Instead of comparing it to Strings like "rock" and "paper", I created an array (String validChoices[] = {"rock", "paper", "scissors"};) which holds the valid responses. Then I compared userChoice to the indices of the array.

Here is my new code:

public class rps {
 public static void main(String []args){
      boolean gameRunning = true;
      while(gameRunning) {
          System.out.println("Do you choose rock, paper, or scissors?");
          UserChoice userInput = new UserChoice();
          String userChoice = userInput.userDecide();
          String validChoices[] = {"rock", "paper", "scissors"};
            if(!userChoice.equals(validChoices[0]) && !userChoice.equals(validChoices[1]) && !userChoice.equals(validChoices[2])) {
                System.out.println("'" + userChoice + "'" + " is not a valid choice.");
                System.out.println("Please choose either rock, paper, or scissors.");
                continue;
            }
          System.out.println("You threw: " + userChoice);
          ComputerInput computerChoice = new ComputerInput();
          String computerInput = computerChoice.computerDecide();
          System.out.println("Computer threw: " + computerInput);
          CompareChoices compareResults = new CompareChoices();
          gameRunning = compareResults.compare(userChoice, computerInput);
      }
 }

};

Thanks everyone!

graham650
  • 11
  • 3
  • To avoid an inevitable followup question, read this: [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – resueman Feb 17 '16 at 20:18
  • Your if statement is wrong. It should be using && instead of ||. The expression you have now is always going to be true. – Devman Feb 17 '16 at 20:31

2 Answers2

1

Do not set gameRunning to false, to not use break, use continue, which will ignore the rest of the loop and start the loop again.

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html

JP Moresmau
  • 7,388
  • 17
  • 31
  • Okay, got it working for that part. When I enter an invalid response (i.e I used banana) it restarts the loop and asks me again. But when I enter a valid response (i.e rock) it still says that it is an invalid response and restarts the loop. – graham650 Feb 17 '16 at 20:52
0

A couple of problems. setting your loop condition to false and breaking leaves no way to restart the loop. Also, as @resueman hinted at. Use .equals() instead of == for comparing strings. My suggestion is to put the error checking inside another while loop.

while(!userChoice.equals("rock") && !userChoice.equals("paper") && !userChoice.equals("scissors")) {
  System.out.println("Your choice was invalid, try again");
  userChoice = userInput.userDecide();
}

Also, you should maybe declare some of this stuff outside your while loop (I am thinking of your ComputerInput, UserInput and CompareChoices objects).

Devman
  • 310
  • 2
  • 10
  • thanks so much!!!! I figured out the && and continue part of it, but my real problem was the comparing operators. instead of != I used the .equals method. It works like a charm now! – graham650 Feb 17 '16 at 21:02