Results 1 to 6 of 6
Thread: How to end loop for game?
- 10-18-2011, 01:18 AM #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(); } } }
- 10-18-2011, 01:40 AM #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.
- 10-18-2011, 01:50 AM #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.
- 10-18-2011, 01:53 AM #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.
- 10-18-2011, 01:55 AM #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());
- 10-18-2011, 01:56 AM #6
Member
- Join Date
- Oct 2011
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
HiQ game ENDLESS LOOP HELP PLEASE !!
By Bgreen7887 in forum New To JavaReplies: 2Last Post: 10-08-2011, 01:27 AM -
What's wrong with this game loop?
By Jacob_ in forum Java GamingReplies: 1Last Post: 06-02-2011, 12:27 AM -
Guessing Game with for loop
By ccart62 in forum New To JavaReplies: 3Last Post: 11-22-2010, 12:55 AM -
Need Some Help, Simple Loop For Hangman Game
By Juo in forum New To JavaReplies: 2Last Post: 11-14-2009, 07:51 PM -
[help] game loop
By Fuzzier in forum Java AppletsReplies: 3Last Post: 10-31-2009, 08:02 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks