public static void main(String args) {
// TODO Auto-generated method stub
int num;
@SuppressWarnings(“resource”)
Scanner scanner = new Scanner(System.in);
System.out.print(“Please enter the number”);
num = scanner.nextInt();
if ((num >= 10 ) && (num <= 100 ))
System.out.println(“SUCESS”);
else;
System.out.println(“PROBLEM”);
}
I came across this problem while doing something else. I f we enter a number less than 10 or greater than 100, we get “PROBLEM” displayed. But any number between 10 and 100 both “SUCCESS” and “PROBLEM” are displayed instead of only “SUCCESS”. Any idea what is wrong or am I missing something?
Hello,
Your problem lies here
else;
Remove the semi-colon.
Regards.
1 Like
This is one of the reasons the Google Java Style Guide recommends always using brackets even when they are not required:
Braces are used with
if
,else
,for
,do
andwhile
statements, even when the body is empty or contains only a single statement.
So your code should look like:
if (num >= 10 && num <= 100) {
System.out.println(“SUCESS”);
} else {
System.out.println(“PROBLEM”);
}
1 Like