Java Else
else 語句
使用 else
語句指定一個程式碼塊,如果條件為 false
則執行。
語法
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
示例
int time = 20;
if (time < 18) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
// Outputs "Good evening."
示例說明
在上面的示例中,時間(20)大於 18,因此條件為 false
。因此,我們繼續執行 else
條件,並向螢幕列印“晚上好”。如果時間小於 18,程式將列印“白天”。