If we indent your code properly and write the (implicit) braces, it becomes apparent what is going on:
public class SecretMessage {
public static void main (String[] args) {
int aValue = 4;
if (aValue > 0){
if (aValue == 0) {
System.out.print("first string");
}
} else /* if (avalue <= 0) */ {
System.out.println("second string");
}
System.out.println("third string");
}
}
With aValue = 4;
, the outer if
is entered (a > 0
), but not the inner if
(a == 0
). Thus, the else
is not entered. Thus only System.out.println("third string");
gets executed.
Some remarks on your code:
- The inner
if
can never be entered. If the outer if
is entered, then i
is > 0
and can therefore not be == 0
.
- You use
System.out.print(...)
and System.out.println(...)
. I have a feeling that you want to use one or the other. For readability, you can also neglect the trailing blanks in those statements.
- the array-brackets (
[]
) should directly follow the type, without a space (String [] args
-> String[] args
).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…