Lanson

V1

2022/11/06阅读:19主题:丘比特忙

大数据必学Java基础(二十二):循环结构

循环结构

一、while

1、语法结构

while (布尔表达式) {

        循环体;

}

在循环刚开始时,会计算一次“布尔表达式”的值,若条件为真,执行循环体。而对于后来每一次额外的循环,都会在开始前重新计算一次。

语句中应有使循环趋向于结束的语句,否则会出现无限循环–––"死"循环。

while循环结构流程图如图所示:

2、练习:1+2+3+4+5

public class TestWhile{
        public static void main(String[] args){
                //功能:1+2+3+4+5
                //1.定义变量:
                int num1 = 1;
                int num2 = 2;
                int num3 = 3;
                int num4 = 4;
                int num5 = 5;
                //2.定义一个求和变量,用来接收和:
                int sum = 0;
                sum += num1;
                sum += num2;
                sum += num3;
                sum += num4;
                sum += num5;
                
                //3.输出和
                System.out.println(sum);
        }
}

上述代码缺点:变量的定义个数太多了

解决:

public class TestWhile{
        public static void main(String[] args){
                //功能:1+2+3+4+5
                //1.定义变量:
                int num = 1;
                //2.定义一个求和变量,用来接收和:
                int sum = 0;
                sum += num;
                num++;
                sum += num;
                num++;
                sum += num;
                num++;
                sum += num;
                num++;
                sum += num;
                num++;
                
                //3.输出和
                System.out.println(sum);
        }
}

上述代码缺点:重复写的代码太多了

解决:---》引入java中循环结构:

public class TestWhile{
        public static void main(String[] args){
                //功能:1+2+3+4+5
                //1.定义变量:
                int num = 1;[1]条件初始化
                //2.定义一个求和变量,用来接收和:
                int sum = 0;              
                while(num<=5){[2]条件判断
                        sum += num;[3]循环体
                        num++;[4]迭代
                }      
                //3.输出和
                System.out.println(sum);
        }
}

3、总结

3.1、循环作用:将部分代码重复执行

循环只是提高了程序员编写代码的效率,但是底层执行的时候依然是重复执行。

3.2、循环四要素

初始化谁,就判断谁,判断谁,就迭代谁

执行过程:[1][2][3][4] [2][3][4] [2][3][4]。。。。

3.3、循环的执行过程

3.4、验证循环的执行过程

3.5、练习

【1】1+2+3+4+5+。。。。+100

【2】2+4+6+8+。。。。+998+1000

【3】5+10+15+20+。。。+100

【4】99+97+95+。。5+3+1

【5】1*3*5*7*9*11*13

public class TestWhile02{
        public static void main(String[] args){
                /*
                【1】1+2+3+4+5+。。。。+100
                int i = 1;
                int sum = 0;
                while(i<=100){
                        sum += i;
                        i++;
                }
                System.out.println(sum);
                【2】2+4+6+8+。。。。+998+1000
                int i = 2;
                int sum = 0;
                while(i<=1000){
                        sum += i;
                        i = i+2;
                }
                System.out.println(sum);
                【3】5+10+15+20+。。。+100
                int i = 5;
                int sum = 0;
                while(i<=100){
                        sum += i;
                        i = i+5;
                }
                System.out.println(sum);
                
                【4】99+97+95+。。5+3+1
                int i = 99;
                int sum = 0;
                while(i>=1){
                        sum += i;
                        i = i-2;
                }
                System.out.println(sum);
                【5】1*3*5*7*9*11*13
                
                */

                int i = 1;
                int result = 1;
                while(i<=13){
                        result *= i;
                        i = i+2;
                }
                System.out.println(result);
        }
}

二、do-while

1、语法结构

do {

        循环体;

} while(布尔表达式) ;

do-while循环结构会先执行循环体,然后再判断布尔表达式的值,若条件为真,执行循环体,当条件为假时结束循环。do-while循环的循环体至少执行一次。do-while循环结构流程图如图所示:

2、代码

public class TestDoWhile{
        public static void main(String[] args){
                //1+2+3+4+...100
                //while方式:
                /*
                int i = 101;
                int sum = 0;
                while(i<=100){
                        sum += i;
                        i++;
                }
                System.out.println(i);//101
                System.out.println(sum);//0
                */

                //do-while方式:
                
                int i = 101;
                int sum = 0;
                do{
                        sum += i;
                        i++;
                }while(i<=100);//一定要注意写这个分号,否则编译出错
                System.out.println(i);//102
                System.out.println(sum);//101
                /*
                【1】while和do-while的区别:
                        while:先判断,再执行
                        do-while:先执行,再判断---》至少被执行一次,从第二次开始才进行判断
                【2】什么场合使用do-while:
                
                while(考试是否通过){
                        考试;
                }
                ---》不合适
                do{
                        考试;
                }while(考试是否通过);
                ---》合适
                */

                
        }
}

三、for

1、语法结构

for (初始表达式; 布尔表达式; 迭代因子) {

      循环体;

}

for循环语句是支持迭代的一种通用结构,是最有效、最灵活的循环结构。for循环在第一次反复之前要进行初始化,即执行初始表达式;随后,对布尔表达式进行判定,若判定结果为true,则执行循环体,否则,终止循环;最后在每一次反复的时候,进行某种形式的“步进”,即执行迭代因子。

  1. 初始化部分设置循环变量的初值
  2. 条件判断部分为任意布尔表达式
  3. 迭代因子控制循环变量的增减

for循环在执行条件判定后,先执行的循环体部分,再执行步进。

for循环结构的流程图如图所示:

2、代码

public class TestFor01{
        public static void main(String[] args){
                //1+2+3+..+100
                //while:
                /*int i = 1;
                int sum = 0;
                while(i<=100){
                        sum += i;
                        i++;
                }
                System.out.println(sum);
                */

                
                //for:
                int sum = 0;
                int i;
                for(i = 1;i<=100;i++){
                        sum += i;
                }
                System.out.println(sum);
                System.out.println(i);
                
                /*
                【1】for的结构:
                for(条件初始化;条件判断;迭代){
                        循环体;
                }
                
                【2】i的作用域:作用范围:离变量最近{}  --->可以自己去控制
                【3】for循环格式特别灵活:格式虽然很灵活,但是我们自己写代码的时候不建议灵活着写。
                for(;;){}  -->死循环
                
                int i = 1;
                for(;i<=100;){
                        sum += i;
                        i++;
                }
                
                【4】死循环:
                for(;;){}
                
                while(true){}
                
                do{
                        
                }while(true);
                
                【5】循环分为两大类:
                第一类:当型   while(){}   for(;;){}
                第二类:直到型  do{}while();
                
                【6】以后常用:for循环 
                【7】do-while,while,for循环谁的效率高?  一样高 
                */

        }
}

四、关键字

在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句。

continue 语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。

return的作用,结束当前所在方法的执行.

1、break

通过练习感受break的作用

作用:停止循环

public class TestFor02{
        public static void main(String[] args){
                //功能:求1-100的和,当和第一次超过300的时候,停止程序
                int sum = 0;
                for(int i=1;i<=100;i++){ 
                        sum += i; 
                        if(sum>300){//当和第一次超过300的时候
                                //停止循环
                                break;//停止循环
                        }
                        System.out.println(sum);
                }
                
        }
}

加深理解

public class TestFor03{
        public static void main(String[] args){
                //break的作用:停止最近的循环
                /*
                for(int i=1;i<=100;i++){
                        System.out.println(i);
                        if(i==36){
                                break;//1-36
                        }
                }
                */

                for(int i=1;i<=100;i++){
                        System.out.println(i);
                        while(i==36){
                                break//1-100  ---》break停止的是while循环,而不是外面的for循环
                        }
                }
        }
}

break带标签的使用

public class TestFor04{
        public static void main(String[] args){
                outer:     ----》定义标签结束的位置
                for(int i=1;i<=100;i++){
                        System.out.println(i);
                        while(i==36){
                                break outer;    ----》根据标签来结束循环 
                        }
                }
        }
}

多层循环也可以使用标签,按照自己的需求去设定即可

2、continue

通过案例感受continue的作用:结束本次循环,继续下一次循环

public class TestFor05{
        public static void main(String[] args){
                //功能:输出1-100中被6整除的数:
                //方式1:
                /*
                for(int i=1;i<=100;i++){ 
                        if(i%6==0){//被6整除
                                System.out.println(i);
                        }
                }
                */

                
                //方式2:
                for(int i=1;i<=100;i++){ 
                        if(i%6!=0){//不被6整除
                                continue;//停止本次循环,继续下一次循环
                        }
                        System.out.println(i);
                }
        }
}

加深理解

public class TestFor06{
        public static void main(String[] args){
                //continue:结束本次离它近的循环,继续下一次循环
                /*
                for(int i=1;i<=100;i++){ 
                        if(i==36){
                                continue;//1-100中间没有36
                        }
                        System.out.println(i);
                }
                */

                
                for(int i=1;i<=100;i++){ 
                        while(i==36){
                                System.out.println("------");
                                continue//1-35+死循环
                        }
                        System.out.println(i);
                }
        }
}

continue带标签的使用

public class TestFor07{
        public static void main(String[] args){
                
                outer:
                for(int i=1;i<=100;i++){ 
                        while(i==36){ 
                                continue outer;  //1-100没有36
                        }
                        System.out.println(i);
                }
        }
}

3、return

return的作用:跟循环无关,就是程序中遇到return那么return所在的那个方法就停止执行了

public class TestFor08{
        public static void main(String[] args){
                //return:遇到return结束当前正在执行的方法
                for(int i=1;i<=100;i++){ 
                        while(i==36){ 
                                return;  
                        }
                        System.out.println(i);
                }
                
                System.out.println("-----");
        }
}

五、循环练习

1、练习1

public class TestFor09{
        public static void main(String[] args){
                /* 输出1-100中被5整除的数,每行输出6个*/
                //引入一个计数器:
                int count = 0;//初始值为0
                for(int i=1;i<=100;i++){
                        if(i%5==0){//被5整除的数
                                System.out.print(i+"\t");
                                count++;//每在控制台输出一个数,count就加1操作
                                if(count%6==0){
                                        System.out.println();//换行
                                }
                        }
                }
        }
}

2、练习2

import java.util.Scanner;
public class TestFor10{
        public static void main(String[] args){
                /*
                        实现一个功能: 
                   【1】请录入10个整数,当输入的数是666的时候,退出程序。
                   【2】判断其中录入正数的个数并输出。
                   【3】判断系统的退出状态:是正常退出还是被迫退出。
                */

                //引入一个计数器:
                int count = 0;
                //引入一个布尔类型的变量:
                boolean flag = true//---》理解为一个“开关”,默认情况下开关是开着的
                Scanner sc = new Scanner(System.in);
                for(int i=1;i<=10;i++){//i:循环次数
                        System.out.println("请录入第"+i+"个数:");
                        int num = sc.nextInt();
                        if(num>0){//录入的正数
                                count++;
                        }
                        if(num==666){
                                flag = false;//当遇到666的时候,“开关”被关上了
                                //退出循环:
                                break;
                        }
                        
                }
                
                System.out.println("你录入的正数的个数为:"+count);
                
                
                if(flag){//flag==true
                        System.out.println("正常退出!");
                }else{//flag==false
                        System.out.println("被迫退出!");
                }
                
                
                
        }
}

六、循环的嵌套使用

1、双重循环

乘法口诀:

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

代码:

public class TestFor11{
    public static void main(String[] args){
                //1*6=6   2*6=12  3*6=18  4*6=24  5*6=30  6*6=36
                /*
                System.out.print("1*6=6"+"\t");
                System.out.print("2*6=12"+"\t");
                System.out.print("3*6=18"+"\t");
                System.out.print("4*6=24"+"\t");
                System.out.print("5*6=30"+"\t");
                System.out.print("6*6=36"+"\t");
                
                for(int i=1;i<=6;i++){
                        System.out.print(i+"*6="+i*6+"\t");
                }
                //换行
                System.out.println();
                
                //1*7=7   2*7=14  3*7=21  4*7=28  5*7=35  6*7=42  7*7=49
                for(int i=1;i<=7;i++){
                        System.out.print(i+"*7="+i*7+"\t");
                }
                //换行
                System.out.println();
                
                //1*8=8   2*8=16  3*8=24  4*8=32  5*8=40  6*8=48  7*8=56  8*8=64
                for(int i=1;i<=8;i++){
                        System.out.print(i+"*8="+i*8+"\t");
                }
                //换行
                System.out.println();
                */

                
                for(int j=1;j<=9;j++){
                        for(int i=1;i<=j;i++){
                                System.out.print(i+"*"+j+"="+i*j+"\t");
                        }
                        //换行
                        System.out.println();
                }
        }
}

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

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*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49

1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36

1*5=5 2*5=10 3*5=15 4*5=20 5*5=25

1*4=4 2*4=8 3*4=12 4*4=16

1*3=3 2*3=6 3*3=9

1*2=2 2*2=4

1*1=1

代码:

public class TestFor12{
    public static void main(String[] args){
                
                /*
                //1*8=8   2*8=16  3*8=24  4*8=32  5*8=40  6*8=48  7*8=56  8*8=64
                for(int i=1;i<=8;i++){
                        System.out.print(i+"*8="+i*8+"\t");
                }
                //换行
                System.out.println();
                
                
                //1*7=7   2*7=14  3*7=21  4*7=28  5*7=35  6*7=42  7*7=49
                for(int i=1;i<=7;i++){
                        System.out.print(i+"*7="+i*7+"\t");
                }
                //换行
                System.out.println();
                 
                //1*6=6   2*6=12  3*6=18  4*6=24  5*6=30  6*6=36
                for(int i=1;i<=6;i++){
                        System.out.print(i+"*6="+i*6+"\t");
                }
                //换行
                System.out.println();
                
                
                
                
                */

                
                for(int j=9;j>=1;j--){
                        for(int i=1;i<=j;i++){
                                System.out.print(i+"*"+j+"="+i*j+"\t");
                        }
                        //换行
                        System.out.println();
                }
        }
}

打印各种形状

长方形:

for(int j=1;j<=4;j++){//j:控制行数
                        //*********
                        for(int i=1;i<=9;i++){//i:控制*的个数
                                System.out.print("*");
                        }
                        //换行:
                        System.out.println();
                }

距离前面有一定空隙的长方形:

for(int j=1;j<=4;j++){//j:控制行数
                        //加入空格:
                        for(int i=1;i<=5;i++){//i:控制空格的个数
                                System.out.print(" ");
                        }
                        //*********
                        for(int i=1;i<=9;i++){//i:控制*的个数
                                System.out.print("*");
                        }
                        //换行:
                        System.out.println();
                }

平行四边形:

for(int j=1;j<=4;j++){//j:控制行数
                        //加入空格:
                        for(int i=1;i<=(9-j);i++){//i:控制空格的个数
                                System.out.print(" ");
                        }
                        //*********
                        for(int i=1;i<=9;i++){//i:控制*的个数
                                System.out.print("*");
                        }
                        //换行:
                        System.out.println();
                }

三角形:

for(int j=1;j<=4;j++){//j:控制行数
                        //加入空格:
                        for(int i=1;i<=(9-j);i++){//i:控制空格的个数
                                System.out.print(" ");
                        }
                        //*********
                        for(int i=1;i<=(2*j-1);i++){//i:控制*的个数
                                System.out.print("*");
                        }
                        //换行:
                        System.out.println();
                }

菱形:

//上面三角形:
                for(int j=1;j<=4;j++){//j:控制行数
                        //加入空格:
                        for(int i=1;i<=(9-j);i++){//i:控制空格的个数
                                System.out.print(" ");
                        }
                        //*********
                        for(int i=1;i<=(2*j-1);i++){//i:控制*的个数
                                System.out.print("*");
                        }
                        //换行:
                        System.out.println();
                }
                
                //下面三角形:
                for(int j=1;j<=3;j++){//j:控制行数
                        //加入空格:
                        for(int i=1;i<=(j+5);i++){//i:控制空格的个数
                                System.out.print(" ");
                        }
                        //*********
                        for(int i=1;i<=(7-2*j);i++){//i:控制*的个数
                                System.out.print("*");
                        }
                        //换行:
                        System.out.println();
                }

空心菱形:

//上面三角形:
                for(int j=1;j<=4;j++){//j:控制行数
                        //加入空格:
                        for(int i=1;i<=(9-j);i++){//i:控制空格的个数
                                System.out.print(" ");
                        }
                        //*********
                        for(int i=1;i<=(2*j-1);i++){//i:控制*的个数
                                if(i==1||i==(2*j-1)){
                                        System.out.print("*");
                                }else{
                                        System.out.print(" ");
                                }
                        }
                        //换行:
                        System.out.println();
                }
                
                //下面三角形:
                for(int j=1;j<=3;j++){//j:控制行数
                        //加入空格:
                        for(int i=1;i<=(j+5);i++){//i:控制空格的个数
                                System.out.print(" ");
                        }
                        //*********
                        for(int i=1;i<=(7-2*j);i++){//i:控制*的个数
                                if(i==1||i==(7-2*j)){
                                        System.out.print("*");
                                }else{
                                        System.out.print(" ");
                                }
                        }
                        //换行:
                        System.out.println();
                }

扩展:菱形打印方式2

实心菱形:

public class TestFor14{
    public static void main(String[] args){
                //先打印出一个正方形,然后某些位置上打印* 某些位置上打印空格:
                int size = 17;
                int startNum = size/2+1;//起始列号
                int endNum = size/2+1;//结束列号
                //引入一个布尔类型的变量---》理解为“开关”
                boolean flag = true;
                for(int j=1;j<=size;j++){
                        //*****
                        for(int i=1;i<=size;i++){
                                if(i>=startNum&&i<=endNum){
                                        System.out.print("*");
                                }else{
                                        System.out.print(" ");
                                }
                        }
                        //换行
                        System.out.println();
                        if(endNum==size){
                                flag = false;
                        }
                        
                        if(flag){//flag是true相当于在菱形的上半侧   flag是false相当于在菱形的下半侧
                                startNum--;
                                endNum++;
                        }else{
                                startNum++;
                            endNum--;
                        }
                }
        }
}

空心菱形:

public class TestFor14{
    public static void main(String[] args){
                //先打印出一个正方形,然后某些位置上打印* 某些位置上打印空格:
                int size = 17;
                int startNum = size/2+1;//起始列号
                int endNum = size/2+1;//结束列号
                //引入一个布尔类型的变量---》理解为“开关”
                boolean flag = true;
                for(int j=1;j<=size;j++){
                        //*****
                        for(int i=1;i<=size;i++){
                                if(i==startNum||i==endNum){
                                        System.out.print("*");
                                }else{
                                        System.out.print(" ");
                                }
                        }
                        //换行
                        System.out.println();
                        if(endNum==size){
                                flag = false;
                        }
                        
                        if(flag){//flag是true相当于在菱形的上半侧   flag是false相当于在菱形的下半侧
                                startNum--;
                                endNum++;
                        }else{
                                startNum++;
                            endNum--;
                        }
                }
        }
}

2、三重循环

百钱买百鸡

二重循环可以帮我们解决:

二元一次方程组的问题:

public class TestFor15{
    public static void main(String[] args){
                for(int a=1;a<=5;a++){
                        for(int b=3;b<=6;b++){
                                if(a+b==7){
                                        System.out.println(a+"----"+b);
                                }
                        }
                }
        }
}

三重循环可以帮我们解决:

三元一次方程组的问题:

public class TestFor16{
    public static void main(String[] args){
                /*
百钱买百鸡:
公鸡5文钱一只,母鸡3文钱一只,小鸡3只一文钱,
用100文钱买一百只鸡,其中公鸡,母鸡,小鸡都必须要有,问公鸡,母鸡,小鸡要买多少只刚好凑足100文钱。

数学:
设未知数:
公鸡:x只
母鸡:y只
小鸡:z只
x+y+z=100只
5x+3y+z/3=100钱
                麻烦方式:
                for(int x=1;x<=100;x++){
                        for(int y=1;y<=100;y++){
                                for(int z=1;z<=100;z++){
                                        if((x+y+z==100)&&(5*x+3*y+z/3==100)&&(z%3==0)){
                                                System.out.println(x+"\t"+y+"\t"+z);
                                        }
                                }
                        }
                }
                */

                //优化:
                for(int x=1;x<=19;x++){
                        for(int y=1;y<=31;y++){
                                int z = 100-x-y;
                                if((5*x+3*y+z/3==100)&&(z%3==0)){
                                        System.out.println(x+"\t"+y+"\t"+z);
                                } 
                        }
                }
        }
}

分类:

后端

标签:

Java

作者介绍

Lanson
V1

CSDN大数据领域博客专家