Results 1 to 3 of 3
  1. #1
    zlass is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Default 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;
    	}
    	
    
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: problem with high low game

    Quote Originally Posted by zlass View Post
    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.
    In what way does your program crash? Does it just terminate or is there some drama involved including scary music? ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default 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 09:53 PM.

Similar Threads

  1. guessing game problem
    By MrM in forum New To Java
    Replies: 3
    Last Post: 02-04-2011, 12:03 AM
  2. Some problem with game
    By kirayamato in forum NetBeans
    Replies: 13
    Last Post: 12-01-2010, 07:06 AM
  3. My java game problem!? Help!!!
    By Jcbconway in forum AWT / Swing
    Replies: 32
    Last Post: 09-26-2010, 10:33 PM
  4. SUDOKU game problem
    By bumblyb33 in forum AWT / Swing
    Replies: 2
    Last Post: 05-06-2009, 12:18 AM
  5. sample of guess high and low game
    By pouria62 in forum AWT / Swing
    Replies: 1
    Last Post: 10-26-2008, 01:57 PM

Posting Permissions

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