Break a loop for java

综合编程 Hello, buddy! (源链)

Is there a way to pause a for loop in java? So is there a way to only go forward one iteration when prompted? I read this http://answers.yahoo.com/question/index?qid=20100212201605AAazS73
and the solution seems like it could have some problems mostly because I don’t fully understand the order the for loop checks its header. The only method I could think of that could accomplish something similar is the following

do {
        if (FLAG) {
            //Do procedure
            i++;
            FLAG = false;
        }
    } while ( i < 6);

When the flag is true the procedure is done and the counter moves forward one. I don’t like this, though, because it will keep looping as long as the counter is below 6, if I am not mistaken. Any ideas?

-Sorry for the lack of clarity. The FLAG in my case would be a static boolean that could be called from another class. The procedure I allude to is dependent on i.

When iterating through a for loop, for example, the one below, it does the following

for (int i = 0; i < 6; i++) {
    // Do stuff
}
  1. It declares the variable i
    and assigns a value of 0
    .
  2. It checks the conditional i < 6
    . If true, then proceed to step 3. Otherwise go to step 6.
  3. Goes through the body of the loop.
  4. Increment the variable i
    due to the i++
    in the for loop header.
  5. Go to step 2.
  6. The loop ends.

As for your objective, I’m not sure what your objective is. Are you looking to pause using a blocking method call? If so, then something like this would work:

for (int i = 0; i < 6; i++) {
    System.in.readLine();
}

Alternatively, you could use some sort of flag that polls to check whether the loop should proceed, such as:

for (int i = 0; i < 6; i++) {
    while (paused) {
        // An infinite loop that keeps on going until the pause flag is set to false
    }
}

Hope this helped.

您可能感兴趣的

CyclicBarrier 简单举例 1 import java.util.concurrent.BrokenBarrierException; 2 import java.util.concurrent.CyclicBarrier; 3 4 import org...
从源码角度彻底搞懂String、StringBuffer、StringBuilder... 遇到好文随手转发~原文地址: https://blog.csdn.net/xfhy_/article/details/80019618 一、引言 学Java很久了,一直处于使用API+查API的状态,不了解原理,久而久之总是觉得很虚,作为一名合格的程序员这是不允许的,不能一直当AP...
AWS SDK for Java 2.0 – Developer Preview We’re pleased to announce the Developer Preview of the AWS SDK for Java 2.0. The 2.0 version of the SDK is a major rewrite of the 1.11.x code base....
UFLO 2.0.0 正式版发布,纯 Java 流程引擎 UFLO第二代中国特色流程引擎,2.0.0版本更新如下: 1uflo设计器bug修复完善流程图显示 2新版的网页流程设计器 3完善javadoc信息,解决新版网页版流程设计器与老版设计器间兼容性问题 4更新使用文档 锐道出品apache2.0开源 广泛应用 中文we...
Keeping your deps tidy My coworker Carmi just published a blog post on the Bazel blog about how Java tracks dependencies. Bazel has some nice facilities built in to let you...
Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » Break a loop for java



专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录