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(); } }