由于 31 > 8,is 應該進入第一個 if 陳述句,并且由于 31 < 100,它應該進入 else 陳述句并輸出“hi”。然而這個程式的輸出是“ther3”。我確實明白這System.out.print("ther3");在兩個 if 陳述句之外,無論如何都會被列印出來。但是為什么System.out.print("hi");不列印。
public static void main(String[] args) {
int x = 31;
int y = 8;
if (x > y) {
if (x > 100) {
System.out.print("Hello");
}
}
else
System.out.print("hi");
System.out.print("ther3");
}
uj5u.com熱心網友回復:
它沒有列印“hi”的原因是該else部分屬于outerif 陳述句 not inner。
您可以看到外部 if 陳述句的花括號{}。它在其他時間之前關閉。這就是為什么它沒有被執行。您可以更改它并查看一次輸出。
if (x > y) {
if (x > 100) {
System.out.print("Hello");
}
}
else
System.out.print("hi");
System.out.print("ther3");
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/437105.html
