1
import java.util.*;


public class Guess
{
    public static void main (String[] args)
    {

        final int MAX=10;
        int answer, guess;

        Scanner scan = new Scanner(System.in);
        Random generator = new Random();

        answer = generator.nextInt(MAX)+1;

        System.out.print ("I'm thinking of a number between 1 and " + MAX + ".Guess what it is: ");

        guess=scan.nextInt();

        if (guess==answer);

            System.out.println (" You got it!");
        else
        { 
            System.out.println("That is not correct");
            System.out.println("The correct answer is"+ answer);
        }
    }
}

When I try to compile this I get an else without if error, I cant see why because I only have one if condition and that else is right after the if. Help please, and also could someone explain this line answer = generator.nextInt(MAX)+1; why did the author from the book added 1?

0

7 Answers 7

4

You have an error there.

if (guess == answer);
    System.out.println(" You got it!");
else {
    System.out.println("That is not correct");
    System.out.println("The correct answer is" + answer);
}

should be

if (guess == answer) {
    System.out.println(" You got it!");
} else {
    System.out.println("That is not correct");
    System.out.println("The correct answer is" + answer);
}

The author added 1 to ensure the value would be at least 1. nextInt(MAX) returns 0 to MAX-1.

Sign up to request clarification or add additional context in comments.

Comments

2

This line is the culprit:

if (guess==answer);

Remove the semicolon; it is terminating the entire if statement.

Comments

2
if (guess==answer); // looks like you threw an extra semicolon in here
        System.out.println (" You got it!");

Just remove the semicolon and you'll be fine.

Help please, and also could someone explain this line answer = generator.nextInt(MAX)+1; why did the author from the book added 1?

generator.nextInt(MAX) will generate a random number between 0 and MAX, and the author wanted it to be at least 1, so he added 1 to it.

1 Comment

It happens to everyone. Some tools (like the NetBeans IDE) can point out semicolons that do nothing, which in this case would have prevented the problem.
1

you have an unecessary semicolon here

if (guess==answer);

Comments

1

Your if statement needs brackets, not a semicolon.

Comments

1

The if statement should look like this:

    if (guess==answer) {
        System.out.println (" You got it!");
    } else
    { 
        System.out.println("That is not correct");
        System.out.println("The correct answer is"+ answer);
    }

The random value is incremented because the random generator creates random numbers between 0 and MAX-1.

Comments

0
  1. Remove the smeioclon after the if condition. You don't necessarily have to have the curly braces since it's just one statement after the condition. If you have more than one then you have to include the curly braces around the statements like you have in your else condition.

  2. Lookup the Random class in the javadocs.

1 Comment

Which if condition? As it stands, your answer is rather vague.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.