Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Play again not working !

  1. #1
    Member
    Join Date
    Jul 2013
    Location
    Baltimore, MD
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Angry Play again not working !

    /*
     * Design and implement an application that plays the Hi-Lo guessing
     * game with numbers.  The program should pick a random number
     * between 1 and 100, and then repeatedly prompt the
     * user to guess the number.  On each guess, report to the user that
     * he/she is correct or that the guess is too high or low.  Continue 
     * accepting guesses until the user guess correctly or quits.  At the end 
     * of the game, ask the user if he/she wants to play again.
     */
     
    package chapterFour;
     
    import java.util.Scanner;
    import java.util.Random;
     
    public class problem8 {
     
    	public static void main(String[] args) {
     
    		Scanner scan = new Scanner(System.in);
     
    		Random rand = new Random();
     
    		String playAgain = "y";
     
    		int num = 0, randomNumber, guesses = 0;
     
    		randomNumber = rand.nextInt(100) + 1;
     
    		while(playAgain.equalsIgnoreCase("y"));
     
    		{
     
    		while(num != randomNumber)
    		{
    			System.out.print("Enter a random number between 1 and 100 to guess : ");
    			num = scan.nextInt();			
     
    			if(num > randomNumber)
    				System.out.println("Too high");
     
    			if(num < randomNumber)
    				System.out.println("Too low");
     
    			else if(num == randomNumber)
    				System.out.print("Correct, ");
     
    			guesses++;
    		}
     
    		System.out.println("It took you " + guesses + " guesses to get it! ");	
     
    		System.out.println("Play again? (y/n)");
    		playAgain = scan.nextLine();
     
    	}
     
    	}
     
    }
    Last edited by pbrockway2; July 3rd, 2013 at 06:59 PM. Reason: code tags added


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Play again not working !

    Hi tonynsx, welcome to the forums!

    I have added code tags to your post. You put [code] at the start of a section of code, and [/code] at the end. That way the code will be formatted properly by the forum software and will be readable.

    It would help if you explained what the problem is. Rather than "not working", say what happens when you run the program and give it specific input.

  3. #3
    Member
    Join Date
    Jul 2013
    Location
    Baltimore, MD
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Play again not working !

    Thank you,

    The problem is, every thing is working as I want, except when it asks "Play again? (y/n)". It asks that question, but it would not let me type in the response (y/n).

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Play again not working !

    Well the program is definitely assigning *some* value to playAgain. You can add some code to see what value is being assigned to that variable.

    System.out.println("Play again? (y/n)");
    playAgain = scan.nextLine();
    System.out.println("playAgain set to -->" + playAgain + "<--");

    The reason for the little arrows is to make it very clear what the value of playAgain is: including an empty string or a string with whitespace in it.

    ---

    To see why you get the output you get with this code have a careful read of the documentation for the two Scanner methods you use: nextInt() and nextLine().

  5. The Following User Says Thank You to pbrockway2 For This Useful Post:

    tonynsx (July 4th, 2013)

  6. #5
    Member
    Join Date
    Jul 2013
    Location
    Baltimore, MD
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Play again not working !

    This is the program with modification, and to see what "playAgain" is set to. Still, it won't let me type in a value when it asks PlayAgain.

    /*
     * Design and implement an application that plays the Hi-Lo guessing
     * game with numbers.  The program should pick a random number
     * between 1 and 100, and then repeatedly prompt the
     * user to guess the number.  On each guess, report to the user that
     * he/she is correct or that the guess is too high or low.  Continue 
     * accepting guesses until the user guess correctly or quits.  At the end 
     * of the game, ask the user if he/she wants to play again.
     */
     
    package chapterFour;
     
    import java.util.Scanner;
    import java.util.Random;
     
    public class problem8 {
     
    	public static void main(String[] args) {
     
    		Scanner scan = new Scanner(System.in);
     
    		Random rand = new Random();
     
    		String playAgain = "n";
     
    		int num = 0, randomNumber, guesses = 0;
     
    		randomNumber = rand.nextInt(100) + 1;
     
    		while(playAgain.equalsIgnoreCase("y"));
     
    		{
     
    		while(num != randomNumber)
    		{
    			System.out.print("Enter a random number between 1 and 100 to guess : ");
    			num = scan.nextInt();			
     
    			if(num > randomNumber)
    				System.out.println("Too high");
     
    			if(num < randomNumber)
    				System.out.println("Too low");
     
    			else if(num == randomNumber)
    				System.out.print("Correct, ");
     
    			guesses++;
    		}
     
    		System.out.println("It took you " + guesses + " guesses to get it! ");	
     
    		System.out.println("Play again? (y/n)");
    		playAgain = scan.nextLine();
    		System.out.println("Play again set to -----> " + playAgain + " <-----");
     
    	}
     
    	}
     
    }

    Enter a random number between 1 and 100 to guess : 67
    Too high
    Enter a random number between 1 and 100 to guess : 45
    Too low
    Enter a random number between 1 and 100 to guess : 55
    Too high
    Enter a random number between 1 and 100 to guess : 50
    Too low
    Enter a random number between 1 and 100 to guess : 52
    Too high
    Enter a random number between 1 and 100 to guess : 51
    Correct, It took you 6 guesses to get it! 
    Play again? (y/n)
    Play again set to ----->  <-----


    --- Update ---

    Here, I've made playAgain an int instead of a string, now it's asking if I want to play again, but if I press 1 or 0, it won't do anything

    /*
     * Design and implement an application that plays the Hi-Lo guessing
     * game with numbers.  The program should pick a random number
     * between 1 and 100, and then repeatedly prompt the
     * user to guess the number.  On each guess, report to the user that
     * he/she is correct or that the guess is too high or low.  Continue 
     * accepting guesses until the user guess correctly or quits.  At the end 
     * of the game, ask the user if he/she wants to play again.
     */
     
    package chapterFour;
     
    import java.util.Scanner;
    import java.util.Random;
     
    public class problem8 {
     
    	public static void main(String[] args) {
     
    		Scanner scan = new Scanner(System.in);
     
    		Random rand = new Random();
     
    		int playAgain = 1;
     
    		int num = 0, randomNumber, guesses = 0;
     
    		randomNumber = rand.nextInt(100) + 1;
     
    		while(playAgain != 1);
     
    		{
     
    		while(num != randomNumber)
    		{
    			System.out.print("Enter a random number between 1 and 100 to guess : ");
    			num = scan.nextInt();			
     
    			if(num > randomNumber)
    				System.out.println("Too high");
     
    			if(num < randomNumber)
    				System.out.println("Too low");
     
    			else if(num == randomNumber)
    				System.out.print("Correct, ");
     
    			guesses++;
    		}
     
    		System.out.println("It took you " + guesses + " guesses to get it! ");	
     
    		System.out.print("Play again? (1 to continue, 0 to quit)) ");
    		playAgain = scan.nextInt();
    		System.out.println("Play again set to -----> " + playAgain + " <-----");
     
    	}
     
    	}
     
    }
    Enter a random number between 1 and 100 to guess : 54
    Too low
    Enter a random number between 1 and 100 to guess : 78
    Too low
    Enter a random number between 1 and 100 to guess : 89
    Too low
    Enter a random number between 1 and 100 to guess : 94
    Too low
    Enter a random number between 1 and 100 to guess : 96
    Too high
    Enter a random number between 1 and 100 to guess : 95
    Correct, It took you 6 guesses to get it! 
    Play again? (1 to continue, 0 to quit)) 1
    Play again set to -----> 1 <-----

  7. #6
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Play again not working !

    The scanner still holds the newLine from entering the last int. NextLine parses that instead of your y/n.
    Add this:
    System.out.println("It took you " + guesses
    					+ " guesses to get it! ");
     
    if(scan.hasNextLine()){
    	scan.nextLine();
    } // etc.

  8. #7
    Member
    Join Date
    Jul 2013
    Location
    Baltimore, MD
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Play again not working !

    Made those changes, now it asks, and accepts (y/n) for answer, but still won't play again if I select 'y'

    /*
     * Design and implement an application that plays the Hi-Lo guessing
     * game with numbers.  The program should pick a random number
     * between 1 and 100, and then repeatedly prompt the
     * user to guess the number.  On each guess, report to the user that
     * he/she is correct or that the guess is too high or low.  Continue 
     * accepting guesses until the user guess correctly or quits.  At the end 
     * of the game, ask the user if he/she wants to play again.
     */
     
    package chapterFour;
     
    import java.util.Scanner;
    import java.util.Random;
     
    public class problem8 {
     
    	public static void main(String[] args) {
     
    		Scanner scan = new Scanner(System.in);
     
    		Random rand = new Random();
     
    		String playAgain = "n";
     
    		int num = 0, randomNumber, guesses = 0;
     
    		randomNumber = rand.nextInt(100) + 1;
     
    		while(playAgain.equalsIgnoreCase("y"));
     
    		{
     
    		while(num != randomNumber)
    		{
    			System.out.print("Enter a random number between 1 and 100 to guess : ");
    			num = scan.nextInt();			
     
    			if(num > randomNumber)
    				System.out.println("Too high");
     
    			if(num < randomNumber)
    				System.out.println("Too low");
     
    			else if(num == randomNumber)
    				System.out.print("Correct, ");
     
    			guesses++;
    		}
     
    		System.out.println("It took you " + guesses + " guesses to get it! ");	
     
    		if(scan.hasNextLine())
    		{
    			scan.nextLine();
    			System.out.print("Play again? (y/n) ");
    			playAgain = scan.nextLine();
    			System.out.println("Play again set to -----> " + playAgain + " <-----");
    		}	
     
     
    	}
     
    	}
     
    }
    Enter a random number between 1 and 100 to guess : 34
    Too high
    Enter a random number between 1 and 100 to guess : 20
    Too low
    Enter a random number between 1 and 100 to guess : 25
    Too low
    Enter a random number between 1 and 100 to guess : 30
    Too low
    Enter a random number between 1 and 100 to guess : 32
    Too high
    Enter a random number between 1 and 100 to guess : 31
    Correct, It took you 6 guesses to get it!
    Play again? (y/n) y
    Play again set to -----> y <-----

     

  9. #8
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Play again not working !

    Of course not, you're never looping the while loop as you have terminated it with the semicolon.

  10. #9
    Member
    Join Date
    Jul 2013
    Location
    Baltimore, MD
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Play again not working !

    Thank you, I've removed the semicolon, and it's looping. I didn't know having a ; would not let it loop.

    --- Update ---

    Now, it's looping and asking if I want to play again, but it' s only looping my "It took you ----- guesses to get it part.

    --- Update ---

    /*
     * Design and implement an application that plays the Hi-Lo guessing
     * game with numbers.  The program should pick a random number
     * between 1 and 100, and then repeatedly prompt the
     * user to guess the number.  On each guess, report to the user that
     * he/she is correct or that the guess is too high or low.  Continue 
     * accepting guesses until the user guess correctly or quits.  At the end 
     * of the game, ask the user if he/she wants to play again.
     */
     
    package chapterFour;
     
    import java.util.Scanner;
    import java.util.Random;
     
    public class problem8 {
     
    	public static void main(String[] args) {
     
    		Scanner scan = new Scanner(System.in);
     
    		Random rand = new Random();
     
    		String playAgain = "y";
     
    		int num = 0, randomNumber, guesses = 0;
     
    		randomNumber = rand.nextInt(10) + 1;
     
    		while(playAgain.equalsIgnoreCase("y"))
     
    		{
     
    		while(num != randomNumber)
    		{
    			System.out.print("Enter a random number between 1 and 10 to guess : ");
    			num = scan.nextInt();			
     
    			if(num > randomNumber)
    				System.out.println("Too high");
     
    			if(num < randomNumber)
    				System.out.println("Too low");
     
    			else if(num == randomNumber)
    				System.out.print("Correct, ");
     
    			guesses++;
    		}
     
    		System.out.println("It took you " + guesses + " guesses to get it! ");	
     
    		if(scan.hasNextLine())
    		{
    			scan.nextLine();
     
    			System.out.print("Play again? (y/n) ");
    			playAgain = scan.nextLine();
    		}	
     
    	}
     
    	}
     
    }

  11. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Play again not working !

    The semi-colon at the end of the first while statement ends the while statement - the while loop - so it will loop, but only between the while( condition ) and the semi-colon, effectively not looping at all.

    For your latest complaint, look at the second while loop and consider what must be done to reinitialize the loop control variables (or the loop condition) so that the game will play again. For instance, isn't a new random number required?

  12. The Following User Says Thank You to GregBrannon For This Useful Post:

    tonynsx (July 4th, 2013)

Similar Threads

  1. Need help getting my animation to play before while loop
    By irishfella in forum Loops & Control Statements
    Replies: 1
    Last Post: February 13th, 2013, 07:46 PM
  2. Creating a class to play sounds.
    By Archibold9 in forum Java Theory & Questions
    Replies: 8
    Last Post: December 21st, 2011, 06:14 PM
  3. Cannot get Java to play a sound...
    By JumpingUpAndDown in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 20th, 2011, 09:35 AM
  4. How to play Audio file?
    By sush in forum Object Oriented Programming
    Replies: 3
    Last Post: June 29th, 2011, 08:11 AM
  5. Sounds to play during action performed
    By smithyboy in forum Object Oriented Programming
    Replies: 2
    Last Post: November 20th, 2010, 04:16 PM