# Thread: How to end loop for game?

1. Member
Join Date
Oct 2011
Posts
3
Rep Power
0

## How to end loop for game?

I'm trying to end a loop for a ro-sham-bo game, but instead of asking for input, the program just breaks. I'm chobo :(
Java Code:
import java.util.Scanner;

public class rps
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
int rock = 1;
int paper = 2;
int scissors = 3;
int input;
String weapon1 = "a";
String weapon2 = "a";
String again = "y";
while (again.equals("Y") || again.equals ("y"))
{
System.out.println ("Rock, paper, scissors! Pick your weapon: Rock = 1, Paper = 2, Scissors = 3");
input = scan.nextInt();
while (input < 1 || input > 3)
{
System.out.println ("NOPE! Pick your weapon: Rock = 1, Paper = 2, Scissors = 3");
input = scan.nextInt();
}

if (input == 1)
weapon1 = "rock";
if (input == 2)
weapon1 = "paper";
if (input == 3)
weapon1 = "scissors";

int opponent = (int)(Math.random() * 3 +1);
if (opponent == 1)
weapon2 = "rock";
if (opponent == 2)
weapon2 = "paper";
if (opponent == 3)
weapon2 = "scissors";

if  (input == opponent)
{
System.out.println ("You both picked " + weapon1+ ". DRAW!");
}

else if (input == 1 && opponent == 2)
{
System.out.println ("You picked " + weapon1+ ". The computer picked " +weapon2+". You should quit life.");
}
else if (input == 1 && opponent == 3)
{
System.out.println ("You picked " +weapon1+ ". The computer picked " +weapon2+". You win!");
}

else if (input == 2 && opponent == 1)
{
System.out.println ("You picked " +weapon1+ ". The computer picked " +weapon2+". You win!");
}
else if (input == 2 && opponent == 3)
{
System.out.println ("You picked " +weapon1+ ". The computer picked " +weapon2+". You win!");
}
else if (input == 3 && opponent == 1)
{
System.out.println ("You picked " +weapon1+ ". The computer picked " +weapon2+". You lose");
}
else if (input == 3 && opponent == 2)
{
System.out.println ("You picked " +weapon1+ ". The computer picked " +weapon1+". You win");
}
System.out.println ("Play again? (Y/N)");
again = scan.nextLine();

}
}
}

2. Member
Join Date
Oct 2011
Posts
83
Rep Power
0

## Re: How to end loop for game?

Is it throwing an exception? If so, telling us exactly what the exception is and on what line it is thrown would be helpful.

3. Member
Join Date
Oct 2011
Posts
3
Rep Power
0

## Re: How to end loop for game?

It doesn't throw an exception. It just ends the program. It won't ask the user to input anything after printing "Play again" and just ends.

4. ## Re: How to end loop for game?

Looks like you have fallen for the pitfall caused by nextInt not consuming an end of line character when a user enters their choice. For a full explanation do a search. Simple solution make a call to nextLine after the call to nextInt.

5. Member
Join Date
Oct 2011
Posts
83
Rep Power
0

## Re: How to end loop for game?

Hmm, I'm not sure, but I suspect the nextInt() method leaves a superfluous newline in the input buffer. To fix, change both instances of scan.nextInt() to Integer.parseInt(scan.nextLine());

6. Member
Join Date
Oct 2011
Posts
3
Rep Power
0

## Re: How to end loop for game?

Originally Posted by Junky
Looks like you have fallen for the pitfall caused by nextInt not consuming an end of line character when a user enters their choice. For a full explanation do a search. Simple solution make a call to nextLine after the call to nextInt.
This did the trick! Thanks guys

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•