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,程序将打印“白天”。