Java 实例 - continue 关键字用法


Java continue 语句语句用来结束当前循环,并进入下一次循环,即仅仅这一次循环结束了,不是所有循环结束了,后边的循环依旧进行。

以下实例使用了 continue 关键字来跳过当前循环并开始下一次循环:

  1. /*
  2. author by shouce.ren
  3. Main.java
  4. */
  5.  
  6. public class Main {
  7. public static void main(String[] args) {
  8. StringBuffer searchstr = new StringBuffer(
  9. "hello how are you. ");
  10. int length = searchstr.length();
  11. int count = 0;
  12. for (int i = 0; i < length; i++) {
  13. if (searchstr.charAt(i) != 'h')
  14. continue;
  15. count++;
  16. searchstr.setCharAt(i, 'h');
  17. }
  18. System.out.println("发现 " + count
  19. + " 个 h 字符");
  20. System.out.println(searchstr);
  21. }
  22. }

以上代码运行输出结果为:

  1. 发现 2 h 字符
  2. hello how are you.