循环结构

循环结构

  • while循环

  • do...while循环

  • for循环

 

  • 在Java5中引入了一种主要用于数组的增强型for循环。


while循环

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

    while(布尔表达式) {
       //循环内容
    }
  • 只要布尔表达式为true,循环就会一直执行下去。

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

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

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

  • 思考:计算1+2+3+...+100=?

package zjystudy.com.struct.cycle;

public class Dome01 {
   public static void main(String[] args) {
       //输出1-100
       int i = 0;
       //死循环
       while(true) {
           //等待客户端连接
           //定时检查
           //...
      }
  }
}
package zjystudy.com.struct.cycle;

public class Dome02 {
   public static void main(String[] args) {
       //计算1+2+3+...+100=?
       
       //高斯的故事
       //首尾相加乘50再除以2
       int i =0 ;
       int sum = 0;

       while(i < 101) {
           sum += i;
           i++;
      }
       System.out.println(sum);
  }
}

do...while循环

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

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

do {
   //代码语句
}while(布尔表达式)
  • while和do...while的区别

    • while先判断后执行。do...while是先执行后判断!

    • do...while总是保证循环循环体会被至少执行一次!这是他们的主要差别。

package zjystudy.com.struct.cycle;

public class Dome03 {
   public static void main(String[] args) {
       int i =0 ;
       int sum = 0;

       do {
           sum += i;
           i++;
      }while (i < 101);

       System.out.println(sum);
  }
}
package zjystudy.com.struct.cycle;

public class Dome04 {
   public static void main(String[] args) {
       int a = 0;
       while (a < 0) {
           System.out.println(a);
           a++;
      }
       System.out.println("****************************");
       do {
           System.out.println(a);
           a++;
      }while (a < 0);
  }
}

For循环

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

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

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

for(初始化;布尔表达式;更新) {
   //代码语句
}
  • 练习1:计算0到100之间的奇数和偶数的和

  • 练习2:用while或者for循环输出1-1000之间能被5整除的数,并且每行输出3个

  • 练习3:打印九九乘法表

package zjystudy.com.struct.cycle;

public class Dome05 {
   public static void main(String[] args) {
       int a = 1; //初始化条件

       while (a <= 100){//条件判断
           System.out.println(a);//循环体
           a += 2;
      }

       System.out.println("while 循环结束!");

       for(int i = 1;i <= 100;i++) {
           System.out.println(i);
      }
       //快捷键100.for + enter
//       for (int i = 0; i < 100; i++) {
//            
//       }
       /*
       * 关于for循环有以下几点说明:
       * 最先执行初始化步骤,可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句
       * 然后,检测布尔表达式的值。如果为true,循环体被执行。如果为false,循环终止
       * ,开始执行循环体后面的语句。
       * 执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)
       * 再次检测布尔表达式,循环执行上面的过程
       * */
       //死循环
       for (;;){
           
      }
  }
}
package zjystudy.com.struct.cycle;

public class Dome06 {
   public static void main(String[] args) {
       //练习1:计算0到100之间的奇数和偶数的和

       int oddsum = 0;
       int evensum = 0;

       for (int i = 0; i < 101; i++) {
           if (i % 2 == 0){
               oddsum += i;
          }else {
               evensum += i;
          }
      }

       System.out.println("奇数的和:" + oddsum);
       System.out.println("偶数的和:" + evensum);
  }
}
package zjystudy.com.struct.cycle;

public class Dome07 {
   public static void main(String[] args) {
       //用while或for循环输出1-1000之间能被5整除的数,并且每行输出三个
       for (int i = 0,count = 0; i < 1001; i++) {
           if((i + 1) % 5 ==0){
               System.out.print(i + 1 + "\t");
               count++;
               if(count % 3 == 0){
                   System.out.println();
              }
          }
      }
       
       //println 输出完会换行
       //print 输出完不全换行
  }
}
package zjystudy.com.struct.cycle;

public class Dome08 {
   public static void main(String[] args) {
       //练习3:打印九九乘法表
       /*
       * 1:我们先打印第一列,这个大家都应该会
       * 2:我们把固定的1再用一个循环包起来
       * 3:去掉重复项:i <= j
       * 4.调整样式
       */
       for (int j = 1; j <= 9; j++) {
           for (int i = 1; i <= j; i++) {
               System.out.print(i + "X" + j + "=" + (j * i) + "\t");
          }
           System.out.println();
      }
  }
}
//Summary:这个程序是for的一层嵌套,是for循环。我们不需要把它看作两层for循环,我们一次只看一层for循环
//。因为每层for循环总有该层for循环的迭代变量,我们只把这个迭代变量视为变量,其他的变量都视为常量!此
// 时我们仅仅研究该层for循环如何写循环体语句。这里暂时不需要考虑重复执行的问题。之后我们将最内层的for
//循环写完了,这时再套比它外一层的for循环,此时将刚才的内循环里视为常量的一部分重新看,然后视为变量
//此时我们先修改内层循环体的这些刚才视为常量的变量,然后相应的修改的当前层判断条件。接着考虑内层循环
// 的判断条件,将某些常量再变为变量,使得代码不再重复。最后完善一下输出的格式,使得其符号题意。
package zjystudy.com.struct.cycle;

import java.util.Arrays;
//根据刚才总结写出的代码,全程没有背的部分,都是按步骤和算法一步一步写的
public class Dome09 {
   public static void main(String[] args) {
       int[] arr = {892,22,23,32,32,873,0,-33,32,-7283};
       System.out.println(Arrays.toString(arr));
       for (int j = 0; j < arr.length - 1; j++) {
           for (int i = 0; i < arr.length - 1 - j; i++) {
               if(arr[i] < arr[i + 1]) {
                   int temp;
                   temp = arr[i];
                   arr[i] = arr[i + 1];
                   arr[i + 1] = temp;
              }
          }
      }
       System.out.println("****************");
       System.out.println(Arrays.toString(arr));
  }
}

增强for循环

  • 这里我们先只是见一面,做个了解,之后数组我们重点使用

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

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

for(声明语句:表达式)
{
   //代码句子
}
  • 声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。

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

package zjystudy.com.struct.cycle;

public class Dome10 {
   public static void main(String[] args) {
       int[] numbers = {80,23,32,87,0,-783,-387};

       for (int i = 0;i < numbers.length;i++){
           System.out.println(numbers[i]);
      }
       System.out.println("*****************");
       //遍历数组的元素
       for (int x:numbers){
           System.out.println(x);
      }
  }
}

break contine

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

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

package zjystudy.com.struct.cycle;

public class BreakDome {
   public static void main(String[] args) {
       int i = 0;
       while (i <100) {
           i++;
           System.out.println(i);
           if (i == 30){
               break;
          }
      }
       System.out.println("123");
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
123
package zjystudy.com.struct.cycle;

public class ContinueDome {
   public static void main(String[] args) {
       int i = 0;
       while (i < 20) {
           i++;
           if (i % 10 == 0) {
               System.out.println();
               continue;
          }
           System.out.println(i);
      }
  }
}
1
2
3
4
5
6
7
8
9

11
12
13
14
15
16
17
18
19

用for实现打印101到150之间所有素数

package zjystudy.com.struct.cycle;

public class LabelDome {
   public static void main(String[] args) {
       //打印101到150之间所有素数
       boolean flag = false;
       for (int j = 101,count = 0;j < 150;j++) {
           for (int i = 2; i < j / 2; i++) {
               flag = true;
               if (j % i == 0) {
                   flag = false;
                   break;
              }
          }
           if (flag) {
               System.out.print(j + "\t");
               count++;
               //Summery:如果需要打印几个数之后换行打印需要一个计数的变量,并且if是需要嵌套的!
               if(count % 4 == 0){
                   System.out.println();
              }
          }
      }
  }
}
package zjystudy.com.struct.cycle;

public class LabeDome02 {
   public static void main(String[] args) {
       //打印101到150之间所有素数
       //素数是指在大于3的自然数中,除了1和它本身以外不再有其他因数的自然数。
       int count = 0;
       //不建议使用,但是可以用
       outer:for(int i = 101;i < 150;i++){
           for (int j = 2;j < i / 2;j++){
               if(i % j == 0){
                   continue outer;
              }
          }
           System.out.print(i + "\t");
      }
  }
}
package zjystudy.com.struct.cycle;

public class TestDome01 {
   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();
      }
  }
}
     *
   ***
  *****
 *******
*********

 

  • 关于goto关键字

    • goto关键字很早就在程序设计语言中出现,尽管goto仍是Java的一个保留字,但并未在语言中得到正式使用;Java没有goto。然而,在break和continue这两个关键字的身上,我们依然能看出一些goto的影子---带标签的break和continue。

    • “标签”是指后面跟一个冒号的标识符,例如:label;

    • 对Java来说唯一用到标签的地方是在循环体之前。而在循环之前设置标签的唯一理由是:我们希望在其中嵌套另一个循环,由于break和continue关键字通常只中断当前循环,但若随同标签使用,它们就会中断到存在标签的地方。

posted @ 2020-10-15 14:00  0基础学Java  阅读(301)  评论(0)    收藏  举报