简体   繁体   English

将 for 循环更改为 while

[英]Change for-loop to a while

How can I change the following code to while-loop?如何将以下代码更改为 while-loop?

for (int i = 12; i < 40; i += 14) {
    for (int j = 30; j > 10; j -= 9) {
        System.out.println(i + "+" + j + "=" + (i + j) + "");
    }
}

I tried this but it's not working:我试过这个,但它不工作:

public class WhileExample {
    public static void main(String[] args) {
        int i = 12;
        int j = 10;
        while (i <= 40) {
            while (j <= 30) {
                System.out.println(i + "+" + j + "=" + (i + j) + "");
                j++;
            }
            i++;
        }
    }
}

The answer you looking for:您正在寻找的答案:

int i = 12;
while (i < 40) {
    int j = 30;
    while (j > 10) {
        System.out.println(i + "+" + j + "=" + (i + j) + "");
        j -= 9;
    }
    i += 14;
}

You have a few issues here.你这里有几个问题。 First, you need to retain the same condition in the for and while loops.首先,您需要在forwhile循环中保留相同的条件。 Second, you need to use the same increment the for loop uses, not just a ++ call.其次,您需要使用与for循环相同的增量,而不仅仅是++调用。 Third, you aren't initializing j like it is in the loop, and the initialization should be done inside the outer loop:第三,你没有像在循环中那样初始化j ,初始化应该在外循环完成:

public class WhileExample {
    public static void main(String[] args) {
        int i = 12;
        while (i < 40) {
            int j = 30;
            while (j > 10) {
                System.out.println(i + "+" + j + "=" + (i + j) + "");
                j -= 9;
            }
            i += 14;
        }
    }
}

Following is the working code to your problem where you aren't seeting up again the value of j to it's default value,以下是您的问题的工作代码,您不会再次将j的值设为默认值,

public class WhileExample {

  public static void main(String[] args) {
    int i = 12;
    int j = 30;
    while (i < 40) {
      while (j > 10) {
        System.out.println(i + "+" + j + "=" + (i + j) + "");
        j -= 9;
      }
      i += 14;
      j = 30;
    }
  }
}

To convert ANY for loop into a while loop, you have to understand the anatomy of both loops.要将任何for循环转换为while循环,您必须了解两个循环的结构。 In general, both loops are constructed as follows:一般来说,两个循环的构造如下:

for-loop for循环

for (loop counter initialization; break condition; counter increment/decrement) {
    body of loop
}

while-loop while循环

while (break condition) {
    body of loop
}

The only thing missing in the while loop is the counter initialization and the counter decrement/increment. while循环中唯一缺少的是计数器初始化和计数器递减/递增。 Typically, when you require loop counters, you will use a for loop.通常,当您需要循环计数器时,您将使用for循环。 When you don't require a loop counter, you will use a while loop.当您不需要循环计数器时,您将使用while循环。 BUT, that is not a rule enforced by the language.但是,这不是语言强制执行的规则。 You can do something like this to use a while loop with a counter variable:您可以执行以下操作来使用带有计数器变量的while循环:

int counter = 0;
while (break condition) {
    body of loop
    counter++;
}

Or, if you want to use a for loop without a counter:或者,如果您想使用不带计数器的for循环:

for (;break condition;) {
    body of loop
}

Or even something crazier like this:甚至像这样更疯狂的东西:

for (;;) {
    body of loop
    if (break condition) {
        break;
    }
}

OBVIOUSLY, the best thing is to use the loops for their intended purpose: for-loops when loop counters are required and while-loops when they are not.显然,最好的办法是将循环用于其预期目的:需要循环计数器时使用 for 循环,不需要时使用 while 循环。

Based on the information here, you should be able to see that:根据此处的信息,您应该能够看到:

for (int i = 12; i < 40; i += 14) {
    for (int j = 30; j > 10; j -= 9) {
        System.out.println(i + "+" + j + "=" + (i + j) + "");
    }
}

Should translate to:应翻译为:

int i = 12;

while (i < 40) {
    int j = 30; // initializing this outside the outer `while` is a logic error
    while (j > 10) {
        System.out.println(i + "+" + j + "=" + (i + j) + "");
        j -= 9;
    }
    i += 14;
}

The easiest way to achieve this conversion is to start from the inside out.实现这种转换的最简单方法是从内到外开始。 Meaning, go to the innermost loop, convert it following my suggestion, and work your way out.意思是,go 到最里面的循环,按照我的建议进行转换,然后解决问题。 No matter how nested your loop is, you should be able to convert any for into a while loop.无论您的循环有多嵌套,您都应该能够将任何for转换为while循环。

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

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