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 2 of 2

Thread: Why is my do/while loop for play again not working?

  1. #1
    Junior Member
    Join Date
    Jun 2019
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Why is my do/while loop for play again not working?

    I am brand new to java. Can someone tell me why my do/while loop for playing again is not working?

    import java.util.Scanner;
     
    public class Lavoie_4_17 {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    		// Declaring and initializing variables.
    		int target_number, user_guess;
    		String play_again = "";
     
    		// Creating scanner for user input.
    		Scanner keyboard = new Scanner(System.in);
     
    		// Do/while loop creating a random number in the range of 1-10 then checking if user wants to play again.
    		do {
    			target_number =  (int)(Math.random()*10 + 1);
     
    			// Do/while loop asking for the user's guess then checking if user's guess is equal to the random number.
    			do {
    				System.out.println("Enter your guess (1-10) in integer form: ");
    				user_guess = keyboard.nextInt();
     
    				// If/else statement comparing user_guess to target number then displaying to the screen whether the guess was
    				// too high, too low, or equal to the target number. If the guess was correct, the user is asked if they would like 
    				// to play again.
    				if (user_guess > target_number) {
    					System.out.println("Too high, try again. ");
    				}
    				else if (user_guess < target_number) {
    					System.out.println("Too low, try again. ");
    				}
    				else {
    					System.out.println("You guessed right!! You won the chance to play again.\nWould you like to play again? (Y/N): ");
    					play_again = keyboard.nextLine();
    				}
     
    			}
     
    			while (target_number != user_guess);
    		}
     
    		while (play_again.equalsIgnoreCase("Y"));
     
    		// Closing keyboard scanner.
    		keyboard.close();
    	}
     
    }


    --- Update ---

    So I added a second scanner and used it for .nextline() input and it now works. Is there a way to clear the scanner so when I create larger programs I don't have to create a ton of scanners for multiple input types?

    import java.util.Scanner;
     
    public class Lavoie_4_17 {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    		// Declaring and initializing variables.
    		int target_number, user_guess;
    		String play_again = "";
     
    		// Creating scanners for user inputs.
    		Scanner keyboard = new Scanner(System.in);
    		Scanner keyboard_again = new Scanner(System.in);
     
    		// Do/while loop creating a random number in the range of 1-10 then checking if user wants to play again.
    		do {
    			target_number =  (int)(Math.random()*10 + 1);
     
    			// Do/while loop asking for the user's guess then checking if user's guess is equal to the random number.
    			do {
    				System.out.println("Enter your guess (1-10) in integer form: ");
    				user_guess = keyboard.nextInt();
     
    				// If/else statement comparing user_guess to target number then displaying to the screen whether the guess was
    				// too high, too low, or equal to the target number. If the guess was correct, the user is asked if they would like 
    				// to play again.
    				if (user_guess > target_number) {
    					System.out.println("Too high, try again. ");
    				}
    				else if (user_guess < target_number) {
    					System.out.println("Too low, try again. ");
    				}
    				else {
    					System.out.println("You guessed right!! You won the chance to play again.\nWould you like to play again? (Y/N): ");
    					play_again = keyboard_again.nextLine();
    				}
     
    			}
     
    			while (target_number != user_guess);
    		}
     
    		while (play_again.equalsIgnoreCase("Y"));
     
    		// Closing keyboard scanners.
    		keyboard.close();
    		keyboard_again.close();
    	}
     
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why is my do/while loop for play again not working?

    Here is one man's discussion: https://christprogramming.wordpress....on-mistakes-1/
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Siylo (June 1st, 2019)

Similar Threads

  1. Play again not working !
    By tonynsx in forum What's Wrong With My Code?
    Replies: 9
    Last Post: July 4th, 2013, 06:46 AM
  2. while loop is not working right
    By Kattracks32 in forum Loops & Control Statements
    Replies: 7
    Last Post: February 20th, 2013, 05:26 AM
  3. 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
  4. Loop not working
    By Lurie1 in forum Loops & Control Statements
    Replies: 12
    Last Post: March 7th, 2012, 06:27 AM
  5. Can someone please tell me why my do while loop isn't working?
    By JavaAsh in forum What's Wrong With My Code?
    Replies: 14
    Last Post: October 20th, 2011, 03:52 PM