Question: (java) Working on a guessing game. But the part where it asks the user to play again isnt working...it just skips right past all that
(java) Working on a guessing game. But the part where it asks the user to play again isnt working...it just skips right past all that and starts the game again without prompting the user as to if they actually do want to play again. Any help would be greatly appreciated. Heres the code.
public class ThisIsPracticeSTILLL { public static int RANGE = 100; public static void main(String[] args) { boolean wantsToPlay = true; Scanner scanner = new Scanner(System.in); do { playgame(); System.out.print("Do you want to play again? "); //this part here isnt working if ((scanner.nextLine().equalsIgnoreCase("y"))) //this part here isnt working { wantsToPlay = true; } else { wantsToPlay = false; } } while (wantsToPlay); }
public static void playgame() { boolean playing = true; Random rand = new Random(); int generatedNumber = rand.nextInt(RANGE); int tries = 0; while (playing) { Scanner input = new Scanner(System.in); System.out.print("Your guess? "); int guess = input.nextInt(); if(guess < generatedNumber) { System.out.println("higher"); tries++; } else if(guess > generatedNumber) { System.out.println("lower"); tries++; } else if(guess == generatedNumber) { System.out.println("You got it right in " + tries + " guesses"); } } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
