7、Java 循环结构

Java, 使.

1for

for(   ){

  

}

:

package com.example.xunhuan;

/**
* @author lin
* @version 1.0
* @date 2020/6/23 23:39
* @Description TODO
*/
public class TestMain {
   static TestMain vo = new TestMain();
   public static void main(String[] args) {
       vo.test();
  }
   public void test() {
       int a[] = {1, 2, 3, 4, 5, 6, 7};
       for (int i = 0; i < a.length; i++) {
           System.out.println("a[i] = " + a[i]);
      }
  }
}

:

a[i] = 1
a[i] = 2
a[i] = 3
a[i] = 4
a[i] = 5
a[i] = 6
a[i] = 7

2whileBooleantruefalse

 while{

    

  }

package com.example.xunhuan;

/**
* @author lin
* @version 1.0
* @date 2020/6/23 23:39
* @Description TODO
*/
public class TestMain2 {

   static TestMain2 vo = new TestMain2();

   public static void main(String[] args) {
       vo.test();
  }

   public void test() {
       int a = 0;
       while (a < 10) {
           System.out.println("a = " + a);
           a++;
      }
  }

}

a = 0
a = 1
a = 2
a = 3
a = 4
a = 5
a = 6
a = 7
a = 8
a = 9

3do-while

 do{

   

  } while

package com.example.xunhuan;

/**
* @author lin
* @version 1.0
* @date 2020/6/23 23:39
* @Description TODO
*/
public class TestMain3 {

   static TestMain3 vo = new TestMain3();

   public static void main(String[] args) {
       vo.test();
  }

   public void test() {
       int a = 0;
       do {
           a++;
           System.out.println("a = " + a);
      } while (a < 10);

  }

}

a = 1
a = 2
a = 3
a = 4
a = 5
a = 6
a = 7
a = 8
a = 9
a = 10

2 demo

for

  1. " " " "

 153 " "153=153

1 for(int i=100;i<1000;i++){
2       int ge=i%10;   //
3       int shi=i/10%10;   //
4       int bai=i/100;   //
5       if(ge*ge*ge+shi*shi*shi+bai*bai*bai==i){
6               System.out,println(""+i);
7           }
8   }

:

153
370
371
407

break

continue

      whilei++continue

returnmainreturn

    return使使returnmain

:

package com.example.xunhuan;

/**
* @author lin
* @version 1.0
* @date 2020/6/23 23:50
* @Description TODO
*/
public class TestMain4 {

   static TestMain4 vo = new TestMain4();

   public static void main(String[] args) {
       vo.test1();
       vo.test2();
       vo.test3();
  }

   public void test1() {
       int[] a = {1, 2, 3, 4, 5, 6, 7};
       int num = 0;
       for (int i : a) {
           if (i == 5) {
               return;
          }
           num++;
      }
       System.out.println("test1() num = " + num);
  }

   public void test2() {
       int[] a = {1, 2, 3, 4, 5, 6, 7};
       int num = 0;
       for (int i : a) {
           if (i == 5) {
               break;
          }
           num++;
      }
       System.out.println("test2(() num = " + num);
  }

   public void test3() {
       int[] a = {1, 2, 3, 4, 5, 6, 7};
       int num = 0;
       for (int i : a) {
           if (i == 5) {
               continue;
          }
           num++;
      }
       System.out.println("test3() num = " + num);
  }


}

:

test2(() num = 4
test3() num = 6

:

使return,. break,continue.

原文地址:https://www.cnblogs.com/naimao/p/13346488.html