zoukankan      html  css  js  c++  java
  • Java知识13 循环结构【多测师】

    一、Java中三种主要的循环结构
    while循环
    do…while循环
    for 循环 --主要用于数组的增强型for循环
    
    二、while循环
    基本结构:
    while( 布尔表达式 ) { 
        //循环内容
    }
    只要布尔值为true就会一直循环下去
    public class Vehicle {
        public static void main(String args[]) {
            int x = 10;
            while( x < 20 ) {
              System.out.print("value of x : " + x );
              x++;
              System.out.print("
    ");
            }
        }
      }
    
    三、do...while循环
    对while语句不满足条件则不进入循环,do...while循环是至少会执行一次
    基本结构:
    do {
           //代码语句
    }while(布尔表达式);
    布尔表达式在循环体后面,所以语句块在检测布尔表达式之前已经执行了,如果布尔表达式的值
    为true,则布尔表达式的值为false
    public class Vehicle {
      public static void main(String args[]) {
       int x = 10;
       do {
        System.out.print("value of x : " + x);
        x++;
        System.out.print("
    ");
       } while (x < 20);
      }
    }
    
    四、for循环
    for循环执行的次数在执行前就确定的 格式如下:
    for(初始化; 布尔表达式; 更新) {
         //代码语句
    }
    for循环注意的地方:
    先执行初始化、可声明为一种类型 可初始化一个或多个循环控制变量 也可以为空语句
    检测布尔表达式的值 如果为true 循环体被执行 为false循环终止  开始执行循环体后面的语句
    执行一次循环后 更新循环控制变量
    public class Vehicle {
        public static void main(String[] args) {
      for(int i=10;i<20;i++)
      System.out.println("value of i:"+i);
        }
    }
    
    五、Java增强for循环
    基本格式如下:
    for(声明语句 : 表达式)
    { 
        //代码句子
    }
    声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配,其作用域限定在循环语句块,其值与此时数组元素的值相等。
    表达式:表达式是要访问的数组名,或者是返回值为数组的方法
    实例如下:
    public class Test {
        public static void main(String args[]) {
      int[] numbers = { 10, 20, 30, 40, 50 };
    
      for (int x : numbers) {
          System.out.print(x);
          System.out.print(",");
      }
      System.out.print("
    ");
      String[] names = { "James", "Larry", "Tom", "Lacy" };
      for (String name : names) {
          System.out.print(name);
          System.out.print(",");
      }
        }
    }
    运行结果:
    10,20,30,40,50,
    James,Larry,Tom,Lacy,
    
    六、break关键字
    break主要用在循环语句或者switch语句中,用来跳出整个语句块
    public class Test {
      public static void main(String args[]) {
       int[] numbers = { 10, 20, 30, 40, 50 };
    
       for (int x : numbers) {
        // x 等于 30 时跳出循环
        if (x == 30) {
         break;
        }
        System.out.print(x);
        System.out.print("
    ");
       }
      }
    }
    运行结果:
    10
    20
    
    七、continue关键字
    continue作用是让程序立刻跳转到下一次循环的迭代
    public class Test {
        public static void main(String args[]) {
      int[] numbers = { 10, 20, 30, 40, 50 };
    
      for (int x : numbers) {
          if (x == 30) {
          continue;
          }
          System.out.print(x);
          System.out.print("
    ");
      }
        }
    }
    运行结果:
    10
    20
    40
    50
    
    八、for循环嵌套
    九九乘法表实例:
    public class Test {
        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 + " ");
          }
          System.out.println();
      }
        }
    }
  • 相关阅读:
    跨域问题
    Django缓存机制
    Django Reat Framework --- 版本控制
    Hellow!
    回忆
    突然看见新评论通知
    [退役前的小声bbbbbbbbbbbbbbbbbbbbbb]
    【题解】【CQOI2018】解锁屏幕(玄学优化)
    solution
    1009-自闭++
  • 原文地址:https://www.cnblogs.com/xiaoshubass/p/13110772.html
Copyright © 2011-2022 走看看