user1721173
user1721173

Reputation: 21

Java rock paper scissors loop

I'm having to make a paper rock scissors program that has the user enter in a choice, then tests it against the computer's choice. After every game, it should ask the player if they want to continue, and they should enter in 'Y' or 'N' to continue or quit. The best I could think was a while loop, and everything works fine except the very last bit.

import java.util.Scanner;

public class rockpaperscissors {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        char cont = 'y';

        while (cont == 'y'){        
            int com = (int)(Math.random() * 3);

            System.out.println("Paper (0), Rock (1), or Scizzors (2)?");
            int hum = input.nextInt();

            if (com==(hum))  
                System.out.println("It's a tie!");

            else if (hum == 0)
            {
                if (com == 1)
                    System.out.println ("You chose paper, computer chose rock You Win!");
                else if (com == 2)
                    System.out.println ("You chose paper, Computer chose scissors You Lose!");
            }

            else if (hum == 1)
            {
                if (com == 2)
                    System.out.println ("You chose Rock, computer chose scissors You Win!");
                else if (com == 0)
                    System.out.println ("You chose Rock, Computer chose paper You Lose!");
            }

            else if (hum == 2)
            {
                if (com == 0)
                    System.out.println ("You chose Scissors, computer chose paper You Win!");
                else if (com == 1)
                    System.out.println ("You chose Scissors, Computer chose rock You Lose!");
            }

            System.out.println("Would you like to continue? (Y/N)");
            cont = input.nextLine().charAt(0);
        }       
    }
}

When I run it, the loop runs fine, the game is played, but then I get a 'string out of index range' error. Any idea how to resolve this?

Upvotes: 2

Views: 16604

Answers (3)

Allen
Allen

Reputation: 1

package rockpaper;


import java.util.Scanner;

/**
 *
 * @author Allen E.
 */

public class RockPaper {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       int rock = 0;
       int paper = 1;
       int Scissors = 2;

       int user = 0;
       int computer = 0;
       int gamesplayed = 0;

       Scanner scan = new Scanner(System.in);

         while (gamesplayed < 3)
                 {


       System.out.println("Rock = 0 , Paper = 1, Scissors = 2");
        String userinput = scan.nextLine();

        int convertinput = Integer.valueOf(userinput);
        int Computerinput = (int)(Math.random()*3);

       if (Computerinput == 1 && convertinput == 0)
       {
           System.out.println("Paper beats Rock " + 
                   "\nThe computer won");
           gamesplayed++;
           computer++;
       }
       else if (convertinput == 1 && Computerinput == 0)
       {
           System.out.println("Paper beats Rock " + 
                   "\nYou Win!");
           gamesplayed++;
           user++;
       }
     if (Computerinput == 0 && convertinput == 2)
     {
         System.out.println("Rock beats Scissors " +
                 "\nThe computer won");
         gamesplayed++;
         computer++;
     }
     else if (convertinput == 0 && Computerinput == 2)
     {
         System.out.println("Rock beats Scissors " +
                 "\nYou Win!");
         gamesplayed++;
         user++;
     }

     if (Computerinput == 2 && convertinput == 1)
     {
         System.out.println("Scissors beats Paper " +
                 "\nThe computer won");
         gamesplayed++;
         computer++;
     }
     else if (convertinput == 2 && Computerinput == 1 )
     {
         System.out.println("Scissors beats Paper " +
                 "\nYou Win");
         gamesplayed++;
         user++;
     }

     /*************************************************
      *                                               *
      *                                               *
      *                 Handling a tie                *
      *                                               *
      *                                               *  
      *************************************************/

     if (Computerinput == 0 && convertinput == 0)
     {
         System.out.println("Rock ties Rock " +
                 "\nTie");

     }
     if (Computerinput == 1 && convertinput == 1)
     {
         System.out.println("Paper ties Paper " +
                 "\nTie");

     }
     if (Computerinput == 2 && convertinput == 2)
     {
         System.out.println("Scissors ties Scissors " +
                 "\nTie");

    }/*End of While Loop*/

     }
    }
}

Upvotes: -1

zmbq
zmbq

Reputation: 39013

Your nextInt() just reads the number from the input buffer, leaving the new line in it. So when you call input.nextLine() you're getting an empty line - the rest of the first line after the number. You should read the next-line and make sure it's not empty. If it is, just read it again.

Incidentally, your code that figures out who won is a bit cumbersome. If I were you, I would try to make it a little more general and clean. Think about a solution that can handle a more complex game, such as Rock Paper Scissors Lizard Spock without adding too much code.

Upvotes: 2

NominSim
NominSim

Reputation: 8511

When you get the answer from the user, you don't read the next line so the scanner still has a new line character. Then when you read the nextline you read that new line, and therefore there is no charat(0).

Change:

cont = input.nextLine().charAt(0);

to:

cont = input.next().charAt(0);

Upvotes: 1

Related Questions