import java.util.Random;
    import java.util.Scanner;
    class indexerad_variable
    {
    public static void main(String[] args)
    {
     
        Random generator = new Random();
        Scanner scan = new Scanner(System.in);
         
        int Tries=0;
        int number = generator.nextInt(100) + 1;
        String igen;
     
        while(true)
        {
        System.out.print("\nNumber 1 - 100: ");
        Tries++;
        int guess = scan.nextInt();
         
        if(guess < number)
        {
        System.out.println("to low");
        }
         
        if(guess > number)
        {
        System.out.println("to high");
        }
         
        if(guess == number)
        {
        System.out.println("Right you have tried "+Tries+" ");
        {
        }
         System.out.print("\nPlay again y/n: ");
         igen = scan.next();
         if (igen.equalsIgnoreCase("N"))
			break;
        }
        }
        }
        }

the program works fine until im going to press y and it return to the last game i dont understan what i need to do for it to work. can anyone help me?

Recommended Answers

All 9 Replies

boolean play = true;
while (play){
// ... run your code
 System.out.print("\nPlay again y/n: ");
 igen = scan.next();
if ( !igen.equalsIgnoreCase("y"))
  play = false;
}

thx but it still returning to the first game:(

what do you mean it still returns to the first game? you mean that the number you have to guess is the same? that's normal, you're not re-setting it's value.

yes that it was i meant. ok how do i do to reset it's value then?

in previous code:

if ( !igen.equalsIgnoreCase("y"))
{
  play = false;
}
else{
  number = generator.nextInt(100) + 1; // chosen to play again, reset value
}

thx for the help:)

Your problem description is a little unclear, but, first I'm noticing that the program only will terminate if "N" is typed, everything else is going to result in another game loop.

Secondly, the number you're guessing is going to be the same for every replay since a new number is not generated in the loop. You need to put the random number generation inside the while loop.

Can you elaborate on exactly what the problem is?

Your problem description is a little unclear, but, first I'm noticing that the program only will terminate if "N" is typed, everything else is going to result in another game loop.

Secondly, the number you're guessing is going to be the same for every replay since a new number is not generated in the loop. You need to put the random number generation inside the while loop.

Can you elaborate on exactly what the problem is?

the problem is: solved two posts earlier.
since the OP wants the user to enter y or n, only quitting on 'n' would be correct, but yes, there might be some more validation there.
as for the number not generated again. read the entire thread, not just the first post.

Oh, I must have started answering before the posts were posted, did not see them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.