• 技术文章 >java >java基础

    continue怎样在java中继续下一个循环?

    小妮浅浅小妮浅浅2021-02-04 12:54:43原创2837

    本教程操作环境:windows7系统、java10版,DELL G3电脑。

    1.定义

    continue语句在循环语句体中,用于结束某次循环,但是其他循环还会继续执行。

    2.语法

    continue 就是循环体中一条简单的语句:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    continue;

    public class ContinueDemo {

        public static void main(String[] args) {

            for (int i = 0; i < 100; i++) {

                if (i % 10 == 0) {

                    System.out.println();

                    continue;

                }

                // 每次遇到整除10的数就换行,同时跳出循环继续下一轮,略过整10的打印

                System.out.print(i + "\t");

            }

        }

    }

    3.实例

    for循环内部的continue语句

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    public class ContinueExample {

      

    public static void main(String args[]){

    for (int j=0; j<=6; j++){

    if (j==4)

    {

    continue;

    }

      

    System.out.print(j+" ");

    }

    }

    }

    输出:

    1

    0 1 2 3 5 6

    continue在while循环中的使用

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    public class ContinueExample2 {

      

    public static void main(String args[]){

    int counter=10;

    while (counter >=0){

    if (counter==7)

    {

    counter--;

    continue;

    }

    System.out.print(counter+" ");

    counter--;

    }

    }

    }

    输出:

    1

    10 9 8 6 5 4 3 2 1 010 9 8 6 5 4 3 2 1 0

    以上就是我们在java中使用continue进行下一个循环的方法,当我们想要终止一个循环,同时让下一个循环运行的时候,就可以选择continue语句来完成。更多Java学习指路:js教程

    专题推荐:java;continue
    上一篇:break如何在java循环中跳出? 下一篇:return返回值如何在java中理解?

    相关文章推荐

    • switch怎样java基础中使用?• java循环控制中for循环怎么用?• break如何在java循环中跳出?

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网