IF…ELSE…IF 语句
if...else if...else 语句用于在多个条件分支中选择执行,只要其中一个分支的条件满足并执行后,就跳出整个 if 语句结构,不再判断后续条件。
格式:
if (条件1) {
// 条件1为 true 时执行
} else if (条件2) {
// 条件1为 false 且条件2为 true 时执行
} else if (条件3) {
// 条件1、2为 false 且条件3为 true 时执行
} else {
// 以上所有条件都为 false 时执行
}Code language: JavaScript (javascript)
练习一:三个整数降序排列
思路:借助临时变量 t 进行两两比较和交换,经过三轮比较后实现 a ≥ b ≥ c。
public static void main(String[] args) {
int a = 1, b = 22, c = 3;
if (b > a) {
int t;
t = b;
b = a;
a = t;
}
if (c > b) {
int t;
t = b;
b = c;
c = t;
}
if (b > a) {
int t;
t = b;
b = a;
a = t;
}
System.out.println(a);
System.out.println(b);
System.out.println(c);
}Code language: JavaScript (javascript)
变量的作用域
变量只在它所在的 {} 代码块内有效。不同代码块中可以使用同名变量:
int a = 5;
if (a > 3) {
int t = 4; // t 只在当前 if 块内有效
}
if (a > 10) {
int t = 6; // 合法,与上面的 t 不在同一个作用域
}Code language: JavaScript (javascript)
注意:不能在同一个作用域内重复声明同名变量。
练习二:输出成绩等级
改进前(使用多个独立 if):
public static void main(String[] args) {
int point;
Scanner scan = new Scanner(System.in);
point = scan.nextInt();
if (point >= 90) {
System.out.println("优秀");
}
if (point >= 80 && point < 90) {
System.out.println("良好");
}
if (point >= 60 && point < 80) {
System.out.println("及格");
}
if (point < 60) {
System.out.println("不及格");
}
}Code language: JavaScript (javascript)
改进后(使用 if...else if...else,更高效):
public static void main(String[] args) {
int point;
Scanner scan = new Scanner(System.in);
point = scan.nextInt();
if (point >= 90) {
System.out.println("优秀");
} else if (point >= 80) {
System.out.println("良好");
} else if (point >= 60) {
System.out.println("及格");
} else {
System.out.println("不及格");
}
}Code language: JavaScript (javascript)
增加输入合法性校验
public static void main(String[] args) {
int point;
Scanner scan = new Scanner(System.in);
point = scan.nextInt();
if (point > 100 || point < 0) {
System.out.println("输入不合法。");
} else {
if (point >= 90) {
System.out.println("优秀");
} else if (point >= 80) {
System.out.println("良好");
} else if (point >= 60) {
System.out.println("及格");
} else {
System.out.println("不及格");
}
}
}Code language: JavaScript (javascript)
练习三:柜台结账系统改为 if-else if 结构
public static void main(String[] args) {
System.out.println("请输入单价:");
Scanner scan = new Scanner(System.in);
double price = scan.nextDouble();
System.out.println("请输入数量:");
double number = scan.nextDouble();
System.out.println("请输入金额:");
double money = scan.nextDouble();
double total = price * number;
if (total >= 1000) {
if (money >= total * 0.9) {
System.out.println("返回:" + (money - total * 0.9));
} else {
System.out.println("抱歉,钱不够");
}
} else if (total >= 500) {
if (money >= total * 0.9) {
System.out.println("返回:" + (money - total * 0.9));
} else {
System.out.println("抱歉,钱不够");
}
} else if (total >= 200) {
if (money >= total * 0.95) {
System.out.println("返回:" + (money - total * 0.95));
} else {
System.out.println("抱歉,钱不够");
}
} else {
if (money >= total) {
System.out.println("返回:" + (money - total));
} else {
System.out.println("抱歉,钱不够");
}
}
}Code language: JavaScript (javascript)
总结:
if-else if-else结构适合多条件互斥的场景,一旦某个条件满足并执行对应分支,后续条件不再判断,效率高于多个独立if语句。
Previous: IF-ELSE 语句
Next: switch 语句