Java开发--3--控制流程

3.1 条件结构

1、if语句逻辑

if (逻辑表达式){             //若逻辑为true,则执行if中的语句,否则if中的语句不执行
	语句1;
	语句2;
}

2、if-else语句逻辑

if (逻辑表达式){            //若逻辑为true,则执行if中的语句
	语句1;
}else{                    //若逻辑为false,则执行else中的语句
	语句2;
}

代码实例

public class test {
    public static void main (String[] agrs){
        int x = 30;
        if( x < 20 ){
            System.out.println("这是 if 语句");
        }else{
            System.out.println("这是 else 语句");          //该语句输出
        }
    }
}

3、if-else语句嵌套

if (逻辑表达式1){            //若逻辑表达式1为true,则执行语句1
	语句1;
}else if (逻辑表达式2){      //若逻辑表达式2为true,则执行语句2
	语句2;
}else if (逻辑表达式3){      //若逻辑表达式3为true,则执行语句3
	语句3;
}else{                     //以上逻辑表达式均为false,则执行语句4
	语句4;
}

代码实例

public class test {
    public static void main (String[] agrs){
        int x = 30;
        if( x == 10 ){
            System.out.println("X是10");
        }else if( x == 20 ){
            System.out.println("X是20");
        }else if( x == 30 ){
            System.out.println("X是30");      //该语句输出
        }else{
            System.out.println("这是 else 语句");
        }
    }
}

4、switch-case-break结构

switch (整型表达式){     //必须是整型表达式(即int/char/long等),不能是浮点型(double)
	case 整型常量值1:    //如果整型表达式值与整型常量值1吻合,则执行该语句块
		语句1;
		语句2;
		break;         //跳出switch结构,若不写break则以此为入口下面语句全都执行
	case 整型常量值2:    //同上,若匹配则执行该代码块。注意整型常量值不能重复
		语句3;
		语句4;
		break;        //break语句建议一直书写
	default:          //若以上整型常量值都没有被匹配,则执行该代码块。default可以省略
		语句5;
}

代码实例

public class test {
    public static void main (String[] agrs){
        char grade = 'C';
        switch(grade){          //变量可以是byte/char/int/short/long/String等
            case 'A' :
                System.out.println("优秀");
                break;
            case 'B' :
                System.out.println("良好");
                break;
            case 'C' :
                System.out.println("及格");            //输出结果:及格
                break;
            case 'D' :
                System.out.println("你需要再努力努力");
                break;
            default :
                System.out.println("未知等级");
        }
        System.out.println("你的等级是 " + grade);     //输出结果:你的等级是C
    }
}

5、Java三目运算符(又称条件运算符)

布尔表达式 ? 表达式1 : 表达式2

先算布尔表达式,若结果为true,则计算表达式1并输出,若结果为false,则计算表达式2并输出

代码实例

public class test {
    public static void main (String[] agrs){
        int a = 100, b = 200;
        int flag = a > b ? a : b;       //a>b不成立,返回值为b的值
        System.out.println(flag);       //输出结果为:200
    }
}

3.2 while循环结构

1、while语句

while (布尔表达式){       
	//先计算布尔表达式,若为真则执行语句块。执行完毕后再次判断布尔表达式
	//若为真则继续执行语句块。以此往复,直到布尔表达式为假跳出while循环
	语句块;
}

代码实例

public class test {
    public static void main (String[] agrs){
        int age = 1;
        while (age <= 100){
            System.out.println("马上有钱");     //输出了100个 "马上有钱"
            age++;
            if (age == 88){             //如果有特殊情况,也可以提前终止while循环
                break;                  //遇到break则跳出循环
            }
        }
        System.out.println("over");          //输出100个马上有钱之后输出over
    }
}

2、do-while语句

do{             //当循环变量初始化与循环结束条件一样时,通常考虑使用do-while
	语句块 ;     //先执行语句块
}while (布尔表达式)       
//再计算布尔表达式,若为true则再次执行语句块。以此往复,直到布尔表达式为假跳出while循环

代码实例

import java.util.Scanner;

public class test {
    public static void main (String[] agrs){
        int pwd;
        do{
            System.out.println("请输入密码");
            Scanner sc = new Scanner(System.in);
            pwd = sc.nextInt();       //接收用户输入的密码赋予变量pwd
        }while (123 != pwd);          //输入不等于123继续输入,直到输入等于123退出
    }
}

3、while和do-while的区别

1.while循环先判断再执行
2.do-while循环先执行一次,再判断。因此循环体在不管任何情况下都会执行一次

3.3 for循环结构

1、for语句形式

表达式1为"初始化表达式",表达式2为"循环的条件",表达式3为"循环变量的改变"

for (表达式1;表达式2(为逻辑表达式);表达式3){        
//先计算表达式1的值,再计算表达式2的值,如果为ture则执行循环体,否则退出循环
    
    语句块(为循环体)                
    //执行循环体。执行完毕之后执行表达式3。之后计算表达式2
    //如果为true则执行循环体,否则退出循环
}

代码实例

public class test {
    public static void main (String[] agrs){
        int sum = 0;
        
        for (int i=1; i<=100; i++){
            sum += i;                  //注意。此处是小写i
            if (sum >= 4000){          //和while循环一样,特殊情况可以终止循环
                break;                 //遇到break则跳出循环
            }
        }
        System.out.println(sum);      //输出结果为1-100的累加和:4005
    }
}

2、for的特殊格式

public class test {
    public static void main (String[] agrs){
        int sum = 0;
        int i = 1;            //原表达式1:初始化表达式
        
        for (;i <= 100;){
        //for中的表达式1和表达式3都可以不写,但封号必须有。
        //同时此处不写不代表绝对不写,它只是将表达式写到外面了。
        //注意:经测试表达式2必须有

            sum += i;
            i++;                 //原表达式3:循环变量的改变
        }
        System.out.println(sum);     //输出结果为1-100的累加和:5050
    }
}

3、coutinue语句和break语句

1.continue只能用于循环中。同时循环中也可以使用break语句。
2.continue作用为跳过循环体中的剩余语句而执行下一次循环。
3.break作用为跳出整个循环不再执行循环,向后继续执行。

代码实例

public class test {
    public static void main (String[] agrs){
        int sum =0;

        for (int i=1; i<=100; i++){
            if (i%10 == 3){      //该语句表示个位为3 的数
                break;           //若符合条件则跳出循环(本循环不再执行),进入后续代码块
            }
            sum += i;            //统计总和时,跳过所有个位为3的数
        }

        for (int i=1; i<=100; i++){
            if (i%10 == 3){     //该语句表示个位为3 的数
                continue;       //若符合条件则跳出本次循环(后面语句不再执行),进入下一次循环
            }
            sum += i;           //统计总和时,跳过所有个位为3的数
        }
    }
}

4、for嵌套循环:以九九乘法表为例

public class test {
    public static void main (String[] agrs){
        for(int i=1;i<=9;i++){        //代表行数。初始为1小于等于9
            for(int j=1;j<=i;j++){    //代表列数。初始为1小于等于i
                System.out.print(j+"*"+i+"="+i*j+" ");
            }
            System.out.println();
        }
    }
}

输出结果

1*1=1 
1*2=2 2*2=4 
1*3=3 2*3=6 3*3=9 
1*4=4 2*4=8 3*4=12 4*4=16 
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章