7 2021-10-12

循环结构

while循环

  • while是最基本的循环,他的结构为:

while(布尔表达式){

循环内容

}

  • 只要布尔表达式为true,循环就会一直执行下去

  • 我们大多数是让循环停止下来的,我们需要一个让表达式失效的方式来结束循环

  • 少部分情况需要循环一直执行,比如服务器的请求相应监听等

  • 循环条件一直为true就会造成无限循环【死循环】,我们正常的业务编程中应该尽量避免死循环。会影响程序性能或者造成程序卡死崩溃

  • 列:打印出1到100

public class CycleTest1 {
    public static void main(String[] args) {
        //输出1-100
        int a = 0;
        while (a < 100) {
            a++;
            System.out.println("输出的内容为:" + a);
        }
    }
}
  • 列:输入一个数求这个数从1加到他本身

import java.util.Scanner;
​
public class CycleTest3 {
    public static void main(String[] args) {
        //输入一个数求这个数从1加到他本身的值是多少
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入一个数据:");
        //接收数据
        //判读数据是否为整数
        if (scanner.hasNextInt()) {
            int a = scanner.nextInt();
            int c = 0;
            int b = 0;
            while (c <= a) {
                b = b + c;
                c++;
            }
            System.out.println("结果" + b);
        } else {
            System.out.println("输入的数据有误请重新输入");
​
        }
        scanner.close();
    }
}
输入一个数据:
43
结果946
​
Process finished with exit code 0

do while循环

  • 对于while语句而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件,也至少执行一次

  • do...while循环和while循环相似,不同的是,do...while循环至少会执行一次。语法:

do{

代码语句

}while(布尔表达式)

  • while和do-while的区别: while先判断后执行。do while是先执行后判断,do while总是保证循环体至少被执行一次,这是他们的主要区别

  • 列:

public class CycleTest4 {
    public static void main(String[] args) {
        //while于do...while的区别后者至少会执行一次
        int a = 0;
        while (a < 0) {
            a++;
        }
        System.out.println(a);
        do {
            a++;
        } while (a < 0);
        System.out.println(a);
    }
}
0
1
​
Process finished with exit code 0

for循环

  • 虽然所有循环结构都可以用while或者do while表示,但Java提供了另一种语句:for循环,使一些循环结构变得更加简单

  • for循环语句是支持迭代的一种通用结构,值最有效的,最灵活的循环结构

  • for循环执行的次数是在执行前就确定的。语法格式如下:

for(初始化;布尔表达式;更新){

代码语句

}

  • 列:计算0到100以内的奇数与偶数的和:

public class CycleTest5 {
    public static void main(String[] args) {
        int jsum = 0;
        int osum = 0;
        for (int a = 0; a <= 100; a++) {
            if (a % 2 == 0) {
                jsum += a;
            } else {
                osum += a;
            }
        }
        System.out.println("偶数和为:" + osum);
        System.out.println("奇数和为:" + jsum);
    }
}
偶数和为:2500
奇数和为:2550
​
Process finished with exit code 0
  • 用for循环属输出1-100之间能被5整除的数,并且每行输出3个

public class CycleTest6 {
    public static void main(String[] args) {
        for (int i = 1; i <= 100; i++) {
            if (i % 5 == 0) {
                System.out.print(i + "\t");
            }
            if (i % (15) == 0) {
                System.out.println();
            }
​
        }
    }
}
5   10  15  
20  25  30  
35  40  45  
50  55  60  
65  70  75  
80  85  90  
95  100 
Process finished with exit code 0
​
  1. if (i % 5 == 0)判断了每隔3个数就换行,这里要巧妙使用

  2. println输出完会换行,print输出完不会换行

  • 打印九九乘法表:

public class CycleTest7 {
    public static void main(String[] args) {
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + "*" + i + "=" + (i * j) + "\t");
            }
            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  
​
Process finished with exit code 0
​
  1. 利用了for循环之间的嵌套打印了乘法表也是循环之间的巧妙应用

用于数组的增强型for循环

  • Java5引入了一种主要用于数组或集合的增强型for循环

  • Java增强for循环语法格式如下:

for(声明语句:表达式){

代码句子

}

  • 声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等

  • 表达式:表达式是要访问的数组名,或者是返回值为数组的方法

  • 列:

public class CycleTest8 {
    public static void main(String[] args) {
        //定义一个数组
        int[] number = {10, 20, 30, 40, 50};
        //用for循环进行遍历
        for (int x : number) {
            //打印出数组
            System.out.println(x);
        }
    }
}
10
20
30
40
50
​
Process finished with exit code 0
​
  1. for (int x : number)数组遍历的写法直接输出x就输出数组number的元素

  • 对比:

public class CycleTest9 {
    public static void main(String[] args) {
        int[] number = {10, 20, 30, 40, 50};
        //利用for循环遍历所有的元素
        for (int i = 0; i < 5; i++) {
            //number[i]中的i表示数组中的第几个元素
            System.out.println(number[i]);
        }
    }
}
10
20
30
40
50
​
Process finished with exit code 0
​
  1. number[i]表示取number数组中的第i个数,还利用了for循环进行遍历

break&continue

  • break在任何循环语句的主体部分,均可用break控制循环的流程。break,用与强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用)

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

  • 列:break

public class CycleTest10 {
    public static void main(String[] args) {
        int i = 0;
        while (i < 100) {
            i = i + 1;
            System.out.println(i);
            if (i == 10) {
                break;
            }
        }
    }
}
1
2
3
4
5
6
7
8
9
10
​
Process finished with exit code 0
  1. 当i等于10是跳出循环

  • 列:continue

public class CycleTest11 {
    public static void main(String[] args) {
        int i = 0;
        while (i < 100) {
            i++;
            if (i % 10 == 0) {
                System.out.println();
                continue;
            }
            System.out.print(i + "\t");
        }
    }
}
1   2   3   4   5   6   7   8   9   
11  12  13  14  15  16  17  18  19  
21  22  23  24  25  26  27  28  29  
31  32  33  34  35  36  37  38  39  
41  42  43  44  45  46  47  48  49  
51  52  53  54  55  56  57  58  59  
61  62  63  64  65  66  67  68  69  
71  72  73  74  75  76  77  78  79  
81  82  83  84  85  86  87  88  89  
91  92  93  94  95  96  97  98  99  
​
Process finished with exit code 0
​
  1. 当i除以10为0时跳过这次循环,这里System.out.println();在continue;前所以会换行

练习

  • 打印三角形

public class CycleTest12 {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            for (int j = 5; j >= i; j--) {
                System.out.print(" ");
            }
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            for (int j = 1; j < i; j++) {
                System.out.print("*");
            }
            System.out.println(" ");
        }
    }
}
     * 
    *** 
   ***** 
  ******* 
 ********* 
​
Process finished with exit code 0
​
  1. 这个题将一个三角形分为3各部分取思考且巧妙使用了for循环的嵌套将三角形的三个部分分别打印出来,在外层for循环增加了println完成换行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值