if(比较表达式) {
语句体1;
}else {
语句体2;
}
---------------------------------------------------
int x = 1;
if(x == 1) {
System.out.println("你好");
}else {
System.out.println("大家好");
}
------------------------------------------------
int x = 1;
int y = 2;
if(x > y) {
System.out.println(x);
}else {
System.out.println(y);
}
---------------------------------------
int num = 10;
if(num % 2 == 0) {
System.out.println(num + "是一个偶数");
}else {
//else 后面不需要 条件表达式, if else是 非此即彼的状态
System.out.println(num + "是一个奇数");
} |
|