条件分支
视频锁定
{$ currentTime | date:'mm:ss' $}
{$ timeLeft | date:'mm:ss' $}
在Java中while循环与if条件分支都是boolean测试,但语义从“只要不下雨就......”改成“如果不下雨就......”。
class IfTest {
public static void main(String[] args) {
int x = 3;
if (x == 3) {
System.out.println("x must be 3");
}
System.out.println("This runs no matter what");
}
}
上面的程序只有在x等于3的情况下才会输出“x must be 3”。而第二条输出语句无论如何都会执行。
将上面的程序稍稍修改一下,我们在前面的例子中加上else条件,因此可以指定“如果下雨在家看电影,否则的话就与Fenby去逛街”。
class IfTest {
public static void main(String[] args) {
int x = 3;
if (x == 3) {
System.out.println("x must be 3");
} else {
System.out.println("x is NOT 3");
}
System.out.println("This runs no matter what");
}
}
在线练习
{$ activeFileHint $}