title: Rock, Paper, Scissors Java Game
body: <p>My assignment is to write a program that lets the user play the game of Rock, Paper, Scissors against the computer.</p>
  
  <p><b>Instructions:</b> </p>
  
  <p>The main method should have two nested loops, where the outer loop will allow the user to play the game as often as desired and the inner loop will play the game as long as there is a tie. Call the method isValidChoice() in a while loop in the userChoice() method to verify that the choice that user enters must be “rock”, “paper”, or “scissors”. If invalid string is input, isValidChoice()will return false and the program should ask for new input until the valid input is given.</p>
  
  <p><b>Situation:</b></p>
  
  <p>The program runs fine when the user enters a valid input. However once its not a valid input, there is a small problem.</p>
  
  <p>Result:</p>
  
  <pre><code>Enter rock, paper, or scissors: rocky
  Invalid input, enter rock, paper, or scissors: roc
  Invalid input, enter rock, paper, or scissors: rock
  The computer's choice was paper
  The user's choice was rocky
  
  Play again? (y/n)
  </code></pre>
  
  <p>As you can see, the program recognizes the invalid inputs. The user finally enter a valid
  input the third time. However, it displays user's first choice "rocky" which is invalid.
  Therefore the program can't display who wins.  </p>
  
  <p><b>Question</b></p>
  
  <p>I need your help.<br>
  <b><i>I want my program to run like this:</i></b> 
  When the user enters multiple invalid inputs, but once 
  a valid input is entered, my program should still be able to display the user's valid input and display the winner.</p>
  
  <pre><code>import java.util.Scanner;
  import java.util.Random;
  
  public class RockPaperScissorsGame 
  {
     public static void main (String[] args)
     {
        Scanner keyboard = new Scanner(System.in);
  
        String computer, user;
        char keepPlaying;
  
        do 
        {
           computer = computerChoice();
           user = userChoice();
  
           System.out.println("The computer's choice was " + computer);
           System.out.println("The user's choice was " + user);
  
           determineWinner(computer, user);
  
           while (computer.equals(user))
           {
              computer = computerChoice();
              user = userChoice();
  
              System.out.println("The computer's choice was " + computer);
              System.out.println("The user's choice was " + user);
  
              determineWinner(computer, user);
           }
  
  
           System.out.println("\n" + "Play again? (y/n)");
           keepPlaying = keyboard.nextLine().toLowerCase().charAt(0);
  
           while ( keepPlaying != 'y' &amp;&amp; keepPlaying != 'n' )
           {
              System.out.println("Invalid input, please enter (y/n)");
              keepPlaying = keyboard.nextLine().toLowerCase().charAt(0);
           }
  
        } while (keepPlaying == 'y');
  
     }
     public static String computerChoice()
     {
        String[] choice = {"rock","paper","scissors"};
        Random rand = new Random();
        int computerChoice = rand.nextInt(3);
        return choice[computerChoice];
     }
     public static String userChoice()
     {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter rock, paper, or scissors: ");
        String choice = keyboard.nextLine();
  
        isValidChoice(choice);
        return choice;
     }
     public static boolean isValidChoice (String choice)
     {
        Scanner keyboard = new Scanner(System.in);
  
        while (!(choice.equalsIgnoreCase("rock")) &amp;&amp; !(choice.equalsIgnoreCase("paper")) &amp;&amp; !(choice.equalsIgnoreCase("scissors")))
      {
         System.out.print("Invalid input, enter rock, paper, or scissors: ");
         choice = keyboard.nextLine();
      }
  
         return true;
      }
      public static void determineWinner(String computer, String user)
      {
         if (computer.equalsIgnoreCase("rock") &amp;&amp; user.equalsIgnoreCase("paper"))
            System.out.println("\n" + "Paper wraps rock.\nThe user wins!");
         else if (computer.equalsIgnoreCase("rock") &amp;&amp; user.equalsIgnoreCase("scissors"))
            System.out.println("\n" + "Rock smashes scissors.\nThe computer wins!");
         else if (computer.equalsIgnoreCase("paper") &amp;&amp; user.equalsIgnoreCase("rock"))
            System.out.println("\n" + "Paper wraps rock.\nThe computer wins!");
         else if (computer.equalsIgnoreCase("paper") &amp;&amp; user.equalsIgnoreCase("scissors"))
            System.out.println("\n" + "Scissors cuts paper.\nThe user wins!");
         else if (computer.equalsIgnoreCase("scissors") &amp;&amp; user.equalsIgnoreCase("rock"))
            System.out.println("\n" + "Rock smashes scissors.\nThe user wins!");
         else if (computer.equalsIgnoreCase("scissors") &amp;&amp; user.equalsIgnoreCase("paper"))
            System.out.println("\n" + "Scissors cuts paper.\nThe computer wins!");
         else if (computer.equalsIgnoreCase(user))
            System.out.println("\n" + "The game is tied!\nGet ready to play again...");
      }   
  }
  </code></pre>