简体   繁体   English

为什么我的while循环不会终止?

[英]Why won't my while loop terminate?

I've noticed when using while loops in my java programs that when using a boolean type to terminate it does not seem to be working. 我注意到在Java程序中使用while循环时,使用布尔类型终止时似乎无法正常工作。 I typed up something simple to test it. 我输入了一些简单的东西来测试它。 The code completes through i = 9 and then test is printed out as false. 代码通过i = 9完成,然后将测试打印为false。

    public class LoopTesting {


    public static void main(String[] args) {

        boolean test = true;

        while(test) {
            for (int i = 1; i < 10; i++) {
                System.out.println(i);
                if(i == 5) test = false;
            }
        }
        System.out.println(test);
    } 
}

EDIT: In response to afzalex's answer I tested this code: 编辑:为响应afzalex的答案,我测试了此代码:

while(test) {
        for (int i = 1; i < 10; i++) {
            System.out.println(i);

        }

        test = false;

        for(int i = 11; i < 20; i++) {
            System.out.println(i);
        }
    }

and it prints to 19. 它打印到19。

You changed test value inside for loop. 您在for循环中更改了test值。 but condition for for loop is i < 10 , not test . 但是for循环的条件是i < 10 ,不是test
So for loop go on iterating until it ends. 因此, for循环,请继续进行迭代直到结束。
Then while is terminated as you had set test as false when control was inside for loop. 然后,当控件位于for循环内时,while被终止,因为您已将test设置为false

This is what you want 这就是你想要的

    while(test) {
        for (int i = 1; i < 10; i++) {
            System.out.println(i);
            if(i == 5) {
             test = false;
             break;
            }
        }
    }

or more simply 或更简单

int i = -1;

while(test) {
        i++;
        System.out.println(i);
        if(i == 5) {
           test = false;
        }
}

The issue with your while loop not closing is because you have an embedded for loop in your code. while循环无法关闭的问题是因为您的代码中嵌入了for循环。 What happens, is your code will enter the while loop, because while(test) will result in true . 发生的是,您的代码将进入while循环,因为while(test)将导致true Then, your code will enter the for loop. 然后,您的代码将进入for循环。 Inside of your for loop, you have the code looping from 1-10. 在for循环中,您的代码从1-10开始循环。 This loop is based off of the Integer value i . 该循环基于Integer数值i The inside loop is basically saying while(i < 10) , so this inside loop is not effected by the boolean value of test. 内部循环基本上是说while(i < 10) ,因此此内部循环不受test的boolean值影响。

 while(test) { // This loop will continue to run until test == false
        for (int i = 1; i < 10; i++) { // This loop will continue to run until i > 9
              test = false; // This will execute on the first loop of the inner loop, but it is not checked at the outer loop until the inner loop is complete
        }
    }

Hope that makes sense. 希望有道理。 Your issue is that the boolean is corresponding to the outer loop, nothing is stopping the inner loop from running. 您的问题是布尔值对应于外部循环,没有什么阻止内部循环运行。

You have an error in your logic. 您的逻辑有误。 The test variable isn't getting checked until your inner for-loop completes. 在内部for循环完成之前,不会检查test变量。 If you only want to iterate 5 times, you can do this: 如果您只想迭代5次,则可以执行以下操作:

while(test) {
    for (int i = 1; i < 10; i++) {
        System.out.println(i);
        if(i == 5){ 
            test = false;
            break;
        }
    }
}

But you probably want to do something like this: 但是您可能想要执行以下操作:

...
int i = 1;
while(test) {
    if(i == 5)
        test = false
    else i++;
}

Here is how you second question code looks like if you replace while and for with a simple if/goto. 如果用简单的if / goto替换一会儿和第二个问题,则第二个问题代码如下所示。 Maybe this is easier to understand for you: 也许这对您来说更容易理解:

loopA: // while
if (test) {
  int i1 = 1;
  loopB: // for 1
  if (i1 < 10) {
    System.out.println(i1);
    i1++;
    goto loopB;
  }

  test = false;

  int i2= 11;
  loopC: // for 2
  if (i2 < 20) {
    System.out.println(i2);
    i2++;
    goto loopC;
  }
  goto loopA;
}
System.out.println(test);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM