zoukankan      html  css  js  c++  java
  • 流程控制与数组

    1 两种基本流程控制结构。

          分支结构:用于实现根据条件选择性的执行某段代码  if  switch

          循环结构:根据循环条件重复执行某段代码   while , do  while , for,

    2 顺序结构

         从上到下逐行执行,中间没有判断,跳转。

    3 分支结构

              if  使用布尔表达式或值作为条件经行分支控制,switch 语句用于对多个整形值经行匹配。

             switch语句后的expression表达式的数据只能是byte  short char int 4个整数类型,和枚举类型,String类型,

    如果省略了case后代码块的break,如果遇到匹配,则会一直执行下去,

     char c = 'c';
            switch (c) {
            case 'a':
                System.out.println('a');
                System.out.println("mama");
                break;
            case 'b':
                System.out.println('b');
                System.out.println("mama");
                break;
            case 'c':
                System.out.println('c');
                System.out.println("mama");
                break;
            case 'd':
                System.out.println('d');
                System.out.println("mama");
                break;
            default:
                break;
            }
    c
    mama
    View Code
          char c = 'c';
            switch (c) {
            case 'a':
                System.out.println('a');
                System.out.println("mama");
                
            case 'b':
                System.out.println('b');
                System.out.println("mama");
            
            case 'c':
                System.out.println('c');
                System.out.println("mama");
                
            case 'd':
                System.out.println('d');
                System.out.println("mama");
            
            default:
                
            }
    c
    mama
    d
    mama
    View Code

     4 循环结构:可以在满足循环条件的情况下,反复执行某一段代码。包括初始化语句(一条或多条),循环条件(boolean表达式),循环体,迭代语句。

    while循环   while(count < 10); {循环体}  由于while后有分号,表名是一个空语句,循环体不会执行。

    do while 循环  while后必须有分号,表示循环结束

    for 循环 如果要在初始化表达式中声明多个变量,则类型要相同。for (int a = 0, b = 1; a < 10; a++);

    控制循环结构 continue  break return

    break不仅可以结束其所在的循环,还可以结束外层循环,break后加标签,标识一个外层循环。标签只有放在循环语句前才有用。

        outer:
            for (int a = 0; a < 10; a++) {
                for (int b = 0; b < 10; b++) {
                    if (b == 8) {
                        System.out.println(a + "  " + b);
                        break outer;
                    }
                }
            }
    View Code

    continue后也可以跟一个标签,用于跳过标签所标循环的当次循环剩下语句,重新开始下次循环。

            outer:
            for (int a = 0; a < 10; a++) {
                for (int b = 0; b < 10; b++) { // b值不会超过2
                    if (b == 2) {
                        System.out.println(a + "  " + b);
                        continue outer;
                    }
                }
            }
    View Code

    5 数组

         一个数组中,数组元素的类型是唯一的,数组是引用数据类型。

    定义数组  type【】 arrayname

    初始化数组:为数组元素分配内存空间,并赋初始值。不要同时使用静态和动态初始化

           1静态初始化,程序员显示指定元素初始值,系统决定长度,2 动态初始化,程序员指定数组长,系统为元素分配初值

            int[] a;  // 定义
            a = new int[]{1, 2, 3}; // 静态初始化1
            a = {1,2,3} // 静态初始化2
            a = new int[2]; // 动态初始化
            Object[] b = new String[3]; // 可以是子类 
    View Code

    使用foreach循环迭代数组元素时,并不能改变数组元素,不要对foreach循环变量赋值

            int[] a = {1, 2, 3};
            for (int i : a) {
                i = 1;
                System.out.println(i);
            }
            System.out.println(a[2]);  // 值不变
    1
    1
    1
    3
    View Code
            int[][] a;
            a = new int[2][];
            a[0] = new int[3];
            a[1] = new int[4];  // 0 和1 元素个数不同
            System.out.println(a[0][2]);
            System.out.println(a[1][3]);
    View Code
            Object[][] a = new String[][] { new String[2], new String[] {"dddd"}};
            Object[][] b = { new String[2], new String[] {"dddd"}};
    View Code
  • 相关阅读:
    reaver 破解wifi
    CDOJ 1255 斓少摘苹果 图论 2016_5_14
    CDOJ 1256 打表+数组 统计
    poj 3190 贪心+优先队列优化
    poj 2376 Cleaning Shifts 贪心 区间问题
    poj 3253 Fence Repair 贪心
    poj 3069 贪心+区间问题
    poj 3050 Hopscotch DFS+暴力搜索+set容器
    poj 2718 Smallest Difference(暴力搜索+STL+DFS)
    poj 3187 Backward Digit Sums
  • 原文地址:https://www.cnblogs.com/whesuanfa/p/7524083.html
Copyright © 2011-2022 走看看