Results 1 to 3 of 3
Thread: problem with high low game
- 12-03-2011, 06:54 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 1
- Rep Power
- 0
problem with high low game
Below is my code for a high low game. It keeps crashing around the part where it asks whether i want to play again or not.
I can't see the error, a hint would be appreciated.
Java Code:import java.util.Scanner; import java.util.Random; public class highlow { public static void main(String[] args) { int input =-1; int low=1; int max=1000; int secret=0; boolean playAgain = true; Random randGenerator = new Random(); Scanner keyboard = new Scanner(System.in); boolean victory = false; do { welcome(); secret = randGenerator.nextInt(max)+low; while (!victory) { input = playerInput(keyboard); input = withinRange(input,low,max,keyboard); victory = highOrLow(input,max,low,secret); } playAgain = playAgain(keyboard); }while (playAgain == true); System.exit(0); } private static void welcome() { System.out.println("This is a game of high and low. You(the user), is tasked with guessing the right number.\n" + "The number is secret, and lies between min(1) to max(1000). You have unlimited tries. "); } private static int playerInput(Scanner keyboard) { System.out.println("Input: "); int input = keyboard.nextInt(); return input; } private static int withinRange(int inputIn, int lowIn, int maxIn, Scanner keyboard) { while (inputIn < lowIn || inputIn > maxIn) { System.out.println("Number is out of range. Try again: "); inputIn = playerInput(keyboard); } return inputIn; } private static boolean highOrLow(int inputIn, int maxIn, int lowIn, int secretIn) { boolean victory = false; if (inputIn == secretIn) { System.out.println("You win."); victory = true; } else if (inputIn < secretIn) { System.out.println("Too low."); } else if (inputIn > secretIn) { System.out.println("Too high."); } else System.out.println("Error.!"); return victory; } private static boolean playAgain(Scanner keyboard) { boolean playAgain = false; System.out.println("Do you want to play again? (Y,N)"); char choice = keyboard.nextLine().charAt(0); if (choice =='y' || choice == 'Y') { playAgain = true; } return playAgain; } }
- 12-03-2011, 08:18 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 12-03-2011, 08:51 PM #3
Re: problem with high low game
One problem is that this line assumes that something other then the Enter key has been entered;
char choice = keyboard.nextLine().charAt(0);
What happens on this line if you press Enter and nothing else is entered?
You should get the input first and test if it has at least 1 character.Last edited by Norm; 12-03-2011 at 08:53 PM.
Similar Threads
-
guessing game problem
By MrM in forum New To JavaReplies: 3Last Post: 02-03-2011, 11:03 PM -
Some problem with game
By kirayamato in forum NetBeansReplies: 13Last Post: 12-01-2010, 06:06 AM -
My java game problem!? Help!!!
By Jcbconway in forum AWT / SwingReplies: 32Last Post: 09-26-2010, 09:33 PM -
SUDOKU game problem
By bumblyb33 in forum AWT / SwingReplies: 2Last Post: 05-05-2009, 11:18 PM -
sample of guess high and low game
By pouria62 in forum AWT / SwingReplies: 1Last Post: 10-26-2008, 12:57 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks