JasonLuc1Fer

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

循环结构

  1. while循环

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

    2. 我们大部分情况是会让循环停止下来的,这需要一个让表达式失效的方式来结束循环

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

    4. 循环条件一直为true就会造成死循环,我们正常的业务编程中应尽量避免死循环,因为会影响程序性能或者程序卡死崩溃

    5. package com.jason.struct;
      
      public class WhileDemo1 {
          public static void main(String[] args) {
              //输出1~100
              int i = 0;
              while (i <100){
                  i++;
                  System.out.println(i);
              }
          }
      }
      
    6. 思考:计算1+2+3+...+100=?

    7. package com.jason.struct;
      
      public class WhileDemo2 {
          public static void main(String[] args) {
              //计算1+2+3+..+100=?
              int i = 0;
              int sum = 0;
              while (i <= 100){
                  sum = sum + i;
                  i++;
              }
              System.out.println(sum);
          }
      }
      
  2. do...while循环

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

    2. do...while语句与while语句相似但是它至少会执行一次,它是先执行后判断

    3. package com.jason.struct;
      
      public class DoWhileDemo1 {
          public static void main(String[] args) {
              int i = 0;
              while (i<0){
                  System.out.println(i); //不执行
                  i++;
              }
              System.out.println("====================");
              do {
                  System.out.println(i); // 0
                  i++;
              }while (i <0);
          }
      }
      
  3. for循环

    1. 虽然所有循环结构都可以用while或者do...while表示,但Java提供了另一种语句---for循环,这种循环可以使循环结构变得简单

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

    3. for循环执行的次数是在执行前就确定的

    4. package com.jason.struct;
      
      public class ForDemo1  {
          public static void main(String[] args) {
              int a =1;//初始条件
              while (a <= 100){//条件判断
                  System.out.println(a);//循环体
                  a+=2;//迭代,a = a+2
          }
              System.out.println("while循环结束!");
              //初始化值;条件判断;迭代
              for (int i=1;i<=100;i++) { //在IDEA中可表达为100.for
                  System.out.println(i);
              }
              System.out.println("for循环结束!");
          }
      }
      
    5. 计算0到100之间的奇数之和以及偶数之和

    6. package com.jason.struct;
      
      public class ForDemo2 {
          public static void main(String[] args) {
              int oddSum = 0;
              int evenSum = 0;
              for (int i =0;i < 100;i++){
                  if (i%2 != 0){ //模运算,取余不等于0.则为奇数
                      oddSum +=i; //oddSum = oddSum + i
                  }else{ //取余等于零,则为偶数
                      evenSum +=i;
                  }
              }
              System.out.println("奇数的和:"+oddSum);
              System.out.println("偶数的和:"+evenSum);
          }
      }
      
    7. 用while或for循环输出1~100之前能被5整除的数并且每行输出3个

    8. package com.jason.struct;
      
      public class ForDemo3 {
          public static void main(String[] args) {
              for (int i = 0; i <= 1000; i++) {
                  if (i%5 ==0){
                      System.out.print(i+"\t");
                  }
                  if (i%(5*3)==0){
                      //也可以打System.out.print("\n");
                      //println 输出完之后换行
                      //print 输出完之后不换行
                      System.out.println();
                  }
              }
          }
      }
      
    9. 打印九九乘法表

    10. package com.jason.struct;
      /*
      1*1=1
      2*1=2  2*2=4
      3*1=3  3*2=6  3*3=9
      4*1=4  4*2=8  4*3=12 4*4=16
      5*1=5  5*2=10 5*3=15 5*4=20 5*5=25
      6*1=6  6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
      7*1=7  7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
      8*1=8  8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
      9*1=9  9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
       */
      public class ForDemo4 {
          public static void main(String[] args) {
              //1.先打印第一列 9.for sout(1 + “*” + i + "=" + (1*i))
              //2.把固定的1再用一个循环包起来,使得1变成j
              //3.去掉重复项,i<=j替换i<=9
              //4.调整样式 加入制表符和换行输出
              for (int j = 1; j <= 9; j++) {
                  for (int i = 1; i <= j; i++) {
                  System.out.print(j + "*" + i + "=" + (j * i) + "\t");
              }
                  System.out.println();
              }
          }
      }
      
  4. 在Java5中引入了一种主要用于数组的增强型for循环

  5. package com.jason.struct;
    
    public class ForDemo5 {
        public static void main(String[] args) {
            int[]numbers={10,20,30,40,50}; //定义了一个数组
            for (int i =0;i<5; i++){
                System.out.println(numbers[i]);
            }
            System.out.println("=======================");
            //遍历数组的元素
            for (int x:numbers){
                System.out.println(x);
            }
        }
    }
    
posted on 2021-11-20 21:44  JasonLuc1Fer  阅读(47)  评论(0)    收藏  举报