多分支
1. 多分支的流程图(重要!)
2. 案例演示 1
- 请大家看个案例[If03.java]:
- 输入保国同志的芝麻信用分:
如果:
- 信用分为 100 分时,输出 信用极好;
- 信用分为(80,99]时,输出 信用优秀
- 信用分为[60,80]时,输出 信用一般;
- 其它情况 ,输出 信用 不及格
- 请从键盘输入保国的芝麻信用分,并加以判断
Scanner myScanner = new Scanner(System.in);
System.out.println("请输入信用分(1-100):");
int grade = myScanner.nextInt();
if(grade >=1 && grade <= 100) {
if(grade == 100) {
System.out.println("信用极好");
} else if (grade > 80 && grade <= 99) {
System.out.println("信用优秀");
} else if (grade >= 60 && grade <= 80) {
System.out.println("信用一般");
} else {
System.out.println("信用不及格");
}
} else {
System.out.println("信用分需要在1-100,请重新输入:)");
}
}
3. 案例演示 2
boolean b = true;
if (b == false){
System.out.println("a");
}else if (b){
System.out.println("b");
}else if (!b){
System.out.println("c");
}else{
System.out.println("d");
}
- 最后输出:b,因为在 if-else 多分支条件中,符合一个条件就输出了,后面的 if 条件就不会去被判断了。
- 改成“=”,就是把 false 赋值给 b,b = false; 第一个条件判断中 if 条件为 false,不会进去。第二个 b 为 false,不会进去。第三个 false 取反 为 true,所以执行为 c