【问题标题】:Continue instruction of while loop not executed未执行while循环的继续指令
【发布时间】:2019-05-06 14:21:16
【问题描述】:

while 循环中的 continue 指令未执行。 在 Eclipse 调试中,循环只是在 System.out 之后中断。

任何人都知道这可能是什么原因,有没有办法在 cpu 指令级别进行调试,看看会发生什么?

  do { // recursive calls through element.parents() until the formula pattern (patternFormula) matches
                counter++;
                System.out.println(counter);
                lookupElement = lookupElement.parent();

                String s = replaceArrowTagAndClean(lookupElement.html()); // replace <img .. src=> and return text()

                m = patternFormula.matcher(s);
                if( (found = m.find()) ) {
                    oneMoreLookahead = false;
                    System.out.println("Continue " + counter);
                    continue;
                }
            } while(!found || oneMoreLookahead);

            System.out.println("End");

输出是:
1
2
3
4
继续 4
结束

(抱歉,创建此帖子时遇到了一些麻烦。哈哈)

【问题讨论】:

  • 请出示代码
  • 邮政编码本身,而不是代码图像。你希望我们重写它来测试它吗?
  • if found = m.find() 在第一次迭代后为真?
  • 此时您的 while 循环中的任何条件都不成立。为什么continue; 应该改变它?
  • 您的预期结果是什么?为什么你认为它应该表现得那样?

标签: java while-loop continue


【解决方案1】:

指令continue 将跳过循环中的所有以下代码行并进入下一次迭代。

如果您将 continue 作为循环的最后一行代码,则离开或删除该指令的行为是相同的。

在您的情况下,您可以简单地使用break 指令立即退出循环。

【讨论】:

  • 我错误地认为 continue 会跳转到循环的开头(在 do { 后面)并跳过 while 条件:while(!found || oneMoreLookahead)。不是这样的,谢谢帮助。 (澄清)
猜你喜欢
  • 2015-07-26
  • 2019-02-26
  • 1970-01-01
  • 2014-03-11
  • 1970-01-01
  • 2015-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多