1.结构
if(比较表达式1) {
语句体1;
}else if(比较表达式2) {
语句体2;
}else if(比较表达式3) {
语句体3;
}
...
else {
语句体n+1;
}
2.执行流程
3.主要事项
1:用if语句来取两个整数的最大值:
class Hello2 { public static void main(String[] args) { int x = 10; int y = 20; int max; if (x > y) { max = x; }else { max = y; } System.out.println("max = " + max); } }
结果:
2.用if语句来判断一个键盘录入的数是奇数还是偶数:
import java.util.Scanner; class Hello2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个整数"); int x = sc.nextInt(); if (x % 2 == 0) { System.out.println(x + "是个偶数"); }else { System.out.println(x + "是个奇数"); } } }
结果:
原文:https://www.cnblogs.com/Wangzui1127/p/11152342.html