java声明

Java continue statement is used to skip the current iteration of a loop. Continue statement in java can be used with for, while and do-while loop.

Java Continue语句用于跳过循环的当前迭代。 Java中的Continue语句可以与forwhiledo-while循环一起使用。

Java继续声明 (Java continue statement)

When continue statement is used in a nested loop, it only skips the current execution of the inner loop. Java continue statement can be used with label to skip the current iteration of the outer loop too. Let’s have a look at some continue java statement examples.

在嵌套循环中使用continue语句时,它仅跳过内部循环的当前执行。 Java Continue语句也可以与label一起使用,以跳过外部循环的当前迭代。 让我们看一些继续的Java语句示例。

Java继续循环 (Java continue for loop)

Let’s say we have an array of integers and we want to process only even numbers, here we can use continue loop to skip the processing of odd numbers.

假设我们有一个整数数组 ,并且只想处理偶数,这里我们可以使用continue循环跳过对奇数的处理。

package com.journaldev.java;public class JavaContinueForLoop {public static void main(String[] args) {int[] intArray = { 1, 2, 3, 4, 5, 6, 7 };// we want to process only even entriesfor (int i : intArray) {if (i % 2 != 0)continue;System.out.println("Processing entry " + i);}}}

Java继续while循环 (Java continue while loop)

Let’s say we have an array and we want to process only index numbers divided by 3. We can use java continue statement here with while loop.

假设我们有一个数组,我们只想处理索引数除以3。我们可以在此处使用带有while循环的java continue语句。

package com.journaldev.java;public class JavaContinueWhileLoop {public static void main(String[] args) {int[] intArray = { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };int i = 0;while (i < 10) {if (i % 3 != 0) {i++;continue;}System.out.println("Processing Entry " + intArray[i]);i++;}}}

Java继续执行do-while循环 (Java continue do-while loop)

We can easily replace above while loop code with do-while loop as below. Result and effect of continue statement will be same as above image.

我们可以轻松地将上述while循环代码替换为do-while循环,如下所示。 Continue语句的结果和效果与上图相同。

do {if (i % 3 != 0) {i++;continue;}System.out.println("Processing Entry " + intArray[i]);i++;
} while (i < 10);

Java继续标签 (Java continue label)

Let’s have a look at java continue label example to skip the outer loop processing. We will use two dimensional array in this example and process an element only if all the elements are positive numbers.

让我们看一下Java继续标签示例,以跳过外循环处理。 在此示例中,我们将使用二维数组 ,并且仅当所有元素均为正数时才处理元素。

package com.journaldev.java;import java.util.Arrays;public class JavaContinueLabel {public static void main(String[] args) {int[][] intArr = { { 1, -2, 3 }, { 0, 3 }, { 1, 2, 5 }, { 9, 2, 5 } };process: for (int i = 0; i < intArr.length; i++) {boolean allPositive = true;for (int j = 0; j < intArr[i].length; j++) {if (intArr[i][j] < 0) {allPositive = false;continue process;}}if (allPositive) {// process the arraySystem.out.println("Processing the array of all positive ints. " + Arrays.toString(intArr[i]));}allPositive = true;}}}

Java继续重点 (Java continue important points)

Some important points about java continue statement are;

关于Java continue语句的一些重要要点是:

  1. For simple cases, continue statement can be easily replaced with if-else conditions but when we have multiple if-else conditions then using continue statement makes our code more readable.对于简单的情况,continue语句可以轻松地用if-else条件替换,但是当我们有多个if-else条件时,则使用continue语句会使我们的代码更具可读性。
  2. continue statement comes handy incase of nested loops and to skip a particular record from processing.在嵌套循环的情况下,continue语句很方便,并且可以跳过处理中的特定记录。

I have made a short video explaining java continue statement in detail, you should watch it below.

我制作了一段简短的视频,详细解释了Java继续语句,您应该在下面观看。

演示地址

Reference: Oracle Documentation

参考: Oracle文档

翻译自: https://www.journaldev.com/983/java-continue-statement

java声明

java声明_Java继续声明相关推荐

  1. java 定义泛型变量_Java不应该允许变量声明的泛型类型声明的任何原因?

    假设我们有一个这样的类: public class xx { public interface Foo { T getValue(); void setValue(T value); } public ...

  2. 一个java源文件中可以声明多少个class与编译后会生成多少个字节码文件

    在一个java源文件中可以声明多个class. 但是,只能最多有一个类声明为public的. 而且要求声明为public的类的类名必须与源文件名相同. 编译的过程 编译以后,会生成一个或多个字节码文件 ...

  3. java异常——异常分类+声明已检查异常+如何抛出异常+自定义异常类

    [0]README 0.1) 本文描述+源代码均 转自 core java volume 1, 旨在理解 java异常--异常分类+声明已检查异常+如何抛出异常+自定义异常类 的相关知识: 0.2)异 ...

  4. java 类一定要声明成public_java测试题(四)--答案

    测试题(四)答案 出卷人:王菲菲 时间:120分钟 一.选择题(每题5分,共50分) 1.下面哪个是Java语言中正确的标识符( C ) A.3com B.import C.that D.this 2 ...

  5. [转载] Java中自定义异常的声明与处理

    参考链接: Java中的用户定义异常 #Java中自定义异常的声明与处理 ##一.编写自己的异常类需要注意 1.所有的异常都必须是Throwable 的子类: 2.如果希望写一个检查性异常类,则需要继 ...

  6. 题目:A派生出子类B,B派生出子类C,并且在Java源代码中有如下声明,问以下哪个说法是正确的?()

    题目: A派生出子类B,B派生出子类C,并且在Java源代码中有如下声明: A a0 = new A(); A a1 = new B(); A a2 = new C(); 问以下哪个说法是正确的?() ...

  7. 类Loopy是公共的, 应在名为 Loopy.java 的文件中声明

    代码是这样的: public class Loopy {public static void main(String[] args){int x = 1; System.out.println(&qu ...

  8. 类XXX是公共的, 应在名为 XXX.java 的文件中声明

    类XXX是公共的, 应在名为 XXX.java 的文件中声明 原码如下: public class Bird {//bird类的fly方法public void fly () {System.out. ...

  9. 【Exception】 javax.xml.bind.annotation.adapters 不可见 已在模块 java.xml.bind 中声明, 但该模块不在模块图中

    [Exception] javax.xml.bind.annotation.adapters 不可见 已在模块 java.xml.bind 中声明, 但该模块不在模块图中 一.问题描述 1.将项目中J ...

最新文章

  1. 力扣(LeetCode)刷题,简单题(第13期)
  2. CSDN湘苗培优|保持热情,告别平庸
  3. Too many open files问题解决
  4. Android11模拟定位开发,Android 11 中的位置信息更新
  5. gnuplot_i 文件的说明,翻译成的中文
  6. 编程笔试(解析及代码实现):猴子吃桃。猴子第一天吃了若干个桃子,当即吃了一半,还不解馋,又多吃了一个…的C++、Java、Python、C#等语言代码实现
  7. 优化算法求解复杂约束问题策略(以粒子群算法为例讲解求解复杂约束问题的多种策略)
  8. 【测试点分析】1081 检查密码 (15分)
  9. Java_Web--JDBC 增加记录操作模板
  10. javaweb引用serverlet库
  11. php100视频教程下载(全集),下载地址链接(整理后包涵解压密码)
  12. 21世纪什么最值钱?“人脸”
  13. html蒙版源代码,jquery蒙版控件实现代码_jquery
  14. 标志寄存器FLAGS----小总结
  15. 动手学深度学习 PyTorch版-Day3
  16. amd显卡Linux查看显存,通过软件查看显存参数_显卡_显卡技术应用-中关村在线
  17. Alibaba内部首发“M8级”500页微服务架构手册,GitHub上杀疯了
  18. 清北学堂day1考试
  19. 电脑显示无法加载远程访问连接管理服务器,Win7系统宽带连接出现错误711无法加载远程访问连接管理器服务如何解决?...
  20. 阿里云短信 签名 模板编写

热门文章

  1. 自学linux指令分析-mkdir
  2. jquery mobile将页面内容当成弹框进行显示
  3. UIProgressView的详细使用
  4. Win7 XAMPP apache无法启动的问题
  5. 对DataGridView中的DataGridViewComboBoxColumn有了一点点体会
  6. [转载] python 去除字符串的标点符号 用_Python成为专业人士笔记–String字符串方法
  7. Ubuntu下添加打印机---之寻找设备lpinfo
  8. mysql时区问题解决方案
  9. Entity Framework 与 面向对象
  10. Java调用Javascript、Python算法总结