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?
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.
Lookup the Random class in the
javadocs.