JAVA基础day03:分支结构if-else、switch-case
程序流程控制
分支结构:if-else
- 第一种:
if(aaa){ 满足条件aaa就执行bbb
bbb
}
- 第二种:
if(aaa){ 满足条件aaa就执行bbb,否则就执行BBB
bbb
}else{
BBB
}
- 第三种:
if(aaa){ 满足条件aaa就执行bbb,否则就看条件AAA
bbb
}else if(AAA){ 如果满足AAA就执行BBB,否则再看条件xxx
BBB
}else if(xxx){ 一直往下,直到满足else if(表达式)中,表达式的条件
xxx
}……… 或都不满足,最后默认执行else{zzz}
}else{
zzz
}
代码实例
class IfTest {
public static void main(String[] args) {
//第一种
int myAge = 18;
if(myAge <= 18 || myAge > 35){
System.out.println("Pass!下一个!"); //直接判断,满足则执行
}
System.out.println("检查结束");
//第二种
int myAge1 = 12;
if(myAge1 < 18){
System.out.println("看动画片去!");//满足if则执行,下面跳过
}else{
System.out.println("你可以看成人影视了!");//不满足if则执行else
}
//第三种
int myAge2 = 60;
if(myAge2 < 18){
System.out.println("回家做作业去!");
//满足if则执行,下面跳过,否则执行下一个else if
}else if(myAge2 < 40){
System.out.println("来呀!快活呀!");
//满足else if则执行,下面跳过。否则下一个
}else if(myAge2 < 55){
System.out.println("上楼吧,悠着点!");
//...
}else{
System.out.println("您好!洗澡搓背在1楼!");
//都不满足,直接执行else
}
}
}
让程序接收用户输入
- 从键盘获取不同类型的变量:需要使用Scanner类
- 1.导包:import java.util.Scanner;
- 2.Scanner实例化:Scanner scan = new Scanner(System.in);(new一个对象)
- 3.调用Scanner类的相关方法(next()、nextInt()、…),来获取指定类型的变量
- 输入类型若与要求的类型不匹配(可向上提升也可),会报异常:InputMisMatchException
代码实例
import java.util.Scanner;
class ScannerTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入你的年龄:");
int myAge = scan.nextInt();
System.out.println("你的年龄是:" + myAge);
if(myAge < 18){
System.out.println("回家做作业去!");
}else if(myAge < 40){
System.out.println("来呀!快活呀!");
}else if(myAge < 55){
System.out.println("上楼吧,悠着点!");
}else{
System.out.println("您好!洗澡搓背在1楼!");
}
//拓展
//生成随机数[a,b]公式: (int)(Math.random() * (b - a + 1) + a);
System.out.println("请输入a:");
int a = scan.nextInt();
System.out.println("请输入b:");
int b = scan.nextInt();
int sj = (int)(Math.random() * (b - a + 1) + a);
System.out.println("生成[" + a + "," + b + "]" + "中的随机数为:" + sj);
}
}
if-else结构可嵌套
代码实例
编写一个程序,接收用户输入3个整数,从小到大排序并输出。
import java.util.Scanner;
class IfTest1 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入第一个整数:");
int i1 = scan.nextInt();
System.out.println("请输入第二个整数:");
int i2 = scan.nextInt();
System.out.println("请输入第三个整数:");
int i3 = scan.nextInt();
if(i1 >= i2){ //如果i1>=i2,则只需再判断i3与i1和i2的大小关系
if(i3 >= i1){
System.out.println(i2 + "," + i1 + "," + i3);
}else if(i3 <= i2){
System.out.println(i3 + "," + i2 + "," + i1);
}else{
System.out.println(i2 + "," + i3 + "," + i1);
}
}else{
if(i3 >= i2){
System.out.println(i1 + "," + i2 + "," + i3);
}else if(i3 <= i1){
System.out.println(i3 + "," + i1 + "," + i2);
}else{
System.out.println(i1 + "," + i3 + "," + i2);
}
}
}
}
分支结构:switch-case
- 根据switch(表达式)的值,与case中的常量依次比较,相同则进入case结构执行语句。
- case之后只能跟常量,不能是范围
- 当前case常量匹配后,执行完当前case仍然继续向下执行,且不用匹配下面的case常量(一旦匹配到符合的case,之后的case常量就不用匹配了,直接进入case执行里面的语句。),直到遇到关键字:break跳出结构;或直到末尾default语句。
class SwitchTest {
public static void main (String[] args) {
int number = 2;
switch(number){
case 1:
System.out.println("one");
break;
case 2:
System.out.println("two");
break;
case 3:
System.out.println("three");
break;
default:
System.out.println("other");
//输出结果:two
//如果不加break,则匹配到case 2后,一直执行到default。输出:
// two
// three
// other
}
}
}
-
default: 相当于if-else结构中的else; default结构是可选的
-
switch(表达式),结构中的表达式,只能是如下6种数据类型:byte、short、char、int、枚举类型(JDK5.0新增)、String类型(JDK7.0新增)
。 -
break语句是可选的
-
如果多个case的执行语句相同,则可合并
class SwitchTest1 {
public static void main (String[] args) {
//0-4不及格,5-7都输出“及格”,8-9良好,10优秀
int number = 7;
switch(number){
case 5:
case 6:
case 7:
System.out.println("及格");
break;
case 8:
case 9:
System.out.println("良好");
break;
case 10:
System.out.println("优秀");
break;
default:
System.out.println("不及格");
}
}
}
说明
- 凡是可以使用switch-case结构的,都可以转换为if-else。反之则不成立。因为switch()表达式有要求,if-else适用性范围大
- 写分支结构时,当switch-case和if-else都可用,且case取值情况不多时,优先使用switch-case结构。switch-case执行效率稍高。