The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 304 Weeks Ago, 1 Day Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
How would you add an invalid input to this code in java,
 public static void main(String[] args)
   {
  String playagain;
               String name1;
               String name2;
  char playAgainOrNot;
  int user1Input, user2Input;
  //Rock = 1
  //Paper = 2
  //Scissors = 3
       Scanner keyboard = new Scanner(System.in);
       Scanner input = new Scanner(System.in);
       do
       {
           System.out.println("Player One enter your first name");
           name1 = input.next();
           System.out.println("Player Two enter your first name");
           name2 = input.next();
           System.out.println("Instructions: Type 1 for Rock, 2 for Paper, or 3 for Scissors");
           System.out.println (name1 + " would you like to play rock, paper, or scissors?");
          Â
           user1Input = keyboard.nextInt();
           System.out.println (name2 + " would you like to play rock, paper, or scissors?");
          Â
           user2Input = keyboard.nextInt();
           //if the users choices are both equal
           if (user1Input == user2Input)
           {
               System.out.println("It's a tie!");
           }
           else if (user1Input != user2Input)
           {
               //rock breaks scissors
              if (user1Input == 1 && user2Input == 3)
               {
                   System.out.println(name1 + " chose Rock");
                   System.out.println(name2 + " chose Scissors");
                   System.out.println("Rock Breaks Scissors " + name1 + " wins!");
               }
               else if (user2Input == 1 && user1Input == 3)
               {
                   System.out.println(name1 + " chose Scissors");
                   System.out.println(name2 + " chose Rock");
                   System.out.println("Rock Breaks Scissors " + name2 + " wins!");
               }
               //paper covers rock
               else if (user1Input == 2 && user2Input == 1)
               {
                   System.out.println(name1 + " chose Paper");
                   System.out.println(name2 + " chose Rock");
                   System.out.println("Paper Covers Rock " + name1 + " wins!");
               }
               else if (user2Input == 2 && user1Input == 1)
               {
                   System.out.println(name1 + " chose Rock");
                   System.out.println( name2 + " chose Paper");
                   System.out.println("Paper Covers Rock " + name2 + " wins!");
               }
               //scissors cut paper
               else if (user1Input == 3 && user2Input == 2)
               {
                   System.out.println(name1 + " chose Scissors");
                   System.out.println(name2 + " chose Paper");
                   System.out.println("Scissors cut Paper " + name1 + " wins!");
               }
               else if (user2Input == 3 && user1Input == 2)
               {
                   System.out.println(name1 + " chose Paper");
                   System.out.println(name2 + " chose Scissors");
                   System.out.println("Scissors cut Paper " + name2 + " wins!");
            }
    }
    System.out.println("Would you like to play again ");
             System.out.println("Please enter Y/N");
             playagain = keyboard.next();
             playagain = playagain.toUpperCase();
             playAgainOrNot = playagain.charAt(0);
         }
         //keep the loop running while the user enter Y
         while (playAgainOrNot == 'Y');
----------- Â ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly