0

Im in AP computer science as a high school student. I was bored so i decided to write a rock paper scissor code. It runs a while loop to play a few games in a row and determine a winner, but the first run on the while loop doesnt take in the user input. Its as if the user = scan.nextLine doesnt work on the first run

  Scanner scan = new Scanner (System.in);
  String answer = "none";
  System.out.println("How many games of Rock, Paper, Scissors would you like to play?");
  int games = scan.nextInt();
  int count = 0;
  int computerWins = 0;
  int userWins = 0;
  int computer = 0;
  String user = " ";
  while (count < 10)
  {
    System.out.println("Enter Rock, Paper, or Scissors");
    user = scan.nextLine();
    computer = ((int)(Math.round((Math.random () * 2) + 1)));
    if (computer == 1)
    {
      answer = "Rock";
    }
    else if (computer == 2)
    {
      answer = "Paper";
    }
    else if (computer == 3)
    {
      answer = "Scissors";
    }
  System.out.println("The Computer Chose: " + answer);
   if ((answer.equals("Rock")) && (user.equals("Scissors")))
   {
     System.out.println("The Computer's Rock beats your Scissors");
     computerWins++;
   }
   else if ((answer.equals("Paper")) && (user.equals("Rock")))
   {
     System.out.println("The Computer's Paper beats your Rock");
     computerWins++;
   }
   else if ((answer.equals("Scissors")) && (user.equals("Paper")))
   {
     System.out.println("The Computer's Scissors beat your Paper");
     computerWins++;
   }
   else if (answer.equals(user))
     System.out.println("TIE! Your " + user + " is the same as the Computer's " + answer);
   else
   {
     System.out.println("Your " + user + " beats the Computer's " + answer);
     userWins++;
   }
   count++;
   System.out.println("Score Stands: Computer " + computerWins + " You " + userWins);
  }
Brandon White
  • 937
  • 9
  • 21

2 Answers2

0

Firstly, I think you might find you're using Java.

In terms of your question, this comes up frequently on Stackoverflow (eg. Why can't I enter a string in Scanner(System.in), when calling nextLine()-method?, or practically an exact copy of your problem, Why doesn't this for-loop let me input text the first cycle?). As a high school student, I'm a little surprised you haven't been taught to research what you can before calling on others.

Anyway ... if you use nextInt the scanner will not move to the next line, so when you use nextLine you are actually reading to the end of the same line as your integer.

There are several ways of dealing with this but, for simplicity, you could just use a scan.nextLine statement after you have acquired the integer. So that part of your code would look like this:

System.out.println("How many games of Rock, Paper, Scissors would you like to play?");
int games = scan.nextInt();
scan.nextLine();
Community
  • 1
  • 1
Ambie
  • 4,067
  • 2
  • 11
  • 22
  • Thanks. I did attempt to find something but I'm not skilled enough in computer science to have a succinct search criteria, so I couldn't find anything that actually helped me. Could you go more in depth, or simplify your explanation of why this happens? – SeanTheSaxMan Oct 16 '15 at 01:16
  • @SeanTheSaxMan, I think you're missing my point. That's for you to research. If you can find this site and pose a question here, then you can certainly do a google search. I wonder if this is the cause of the boredom you proclaim. – Ambie Oct 16 '15 at 01:24
0
int games = scan.nextInt();
scan.nextLine();

Just add this portion of code after your nextInt() call to consume the rest of the line.

If this is not done,

user = scan.nextLine();

at this line, user will take the value of the rest of the line where the int was.

Yassin Hajaj
  • 20,020
  • 9
  • 41
  • 81