Problem with a really simple program in java IF statement condition is being ignored
I'm writing a program and my if statement doesn't give the expected output. I searched and found a tutorial on java if statements and copied the code on the tutorial into eclipse.
int value = 1
if (value == 3); {
System.out.println("hello world")
}
The problem is whatever I change the value to, the system still outputs "hello world". It doesn't make much sense to me I've done much more complicated programs in the past. Is there something stupid that I'm missing? Th开发者_高级运维anks Brad. ,
The semicolon in line 2 terminates the if and it should be removed
int value = 1;
if (value == 3) {
System.out.println("hello world");
}
You need to fix your formatting.
int value = 1;
if (value == 3); // extra ;
// totally unrelated block of code.
{
System.out.println("hello world")
}
Which is why I use me IDE to do my formatting.
In addition to the other answers, this code will not compile because you're missing a semicolon when doing variable assignment:
int value = 1;
精彩评论