After guess becomes true, number to guess is not getting next random number, i have to compile it again and again


what I want to do is when guess value becomes true then numberToGuess get new number and user have to guess it untill user want to play game but what happening is when iam compiling the programe and guess becomes true, in place of new number for guess it repeating previous value until i compile it again...

package Ggame;
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Random rand = new Random();
	int numberToGuess = rand.nextInt(10);
	int numberOfTries = 0;
	Scanner input = new Scanner(;
	int guess;
	boolean win = false;
	do	{
		 System.out.println("Enter your Guess between 1 to 10");
		 guess = input.nextInt();
		 if (guess == numberToGuess){
			 win = true;
			 System.out.println("You win");
			 System.out.println("Your Guess:"+numberToGuess);
			 System.out.println("You take:"+numberOfTries+"Tries");
			 numberToGuess = rand.nextInt();
		 else if (guess < numberToGuess){
			 System.out.println("Your Guess is too low");
		 else if(guess > numberToGuess){
			 System.out.println("Your Guess is too High");



You define numberToGuess outside of your loop, so it only generates it once at the start of the program. Move int numberToGuess = rand.nextInt(10) inside the loop, at the top of the loop.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.