java - Guessing game play again trouble -


ok i've tried 1 method kept showing "do want play again" every time entered number reason. here have far. else after user guesses correct answer asks if he/she wants play again?

import java.util.random; import java.util.scanner;  public class guessnumber {      public static void main(string[] args) {          scanner scan = new scanner(system.in);          random rand = new random();         int number = rand.nextint(100) + 1;         int guess;          system.out.println("guess number between 1 , 100\n");          guess = scan.nextint();          while (true) {             if(guess < number)                       system.out.println("higher!");             else if(guess > number)                system.out.println("lower!");             else if (guess == number){                system.out.println("correct!");             }                guess = scan.nextint();        }     }   } 

you need exit while when correct.

to so, after line:

system.out.println("correct!"); 

add line this:

break; 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -