简体   繁体   English

Java为while循环嵌套

[英]Java nested for while loops

I am having troubles with nested while loops in a for loop. 我在for循环中嵌套while循环遇到麻烦。 I understand the nested for loop: 我了解嵌套的for循环:

    for (int i = 0; i<5;i++)
    {
        for (int j=i;j<5;j++)    
        {
           System.out.print("*"); 
        }
        System.out.println();
    }

When it comes to a while loop inside a for loop I'm kinda lost, can someone explain it to me? 当谈到for循环中的while循环时,我有点迷失了,有人可以向我解释吗?

Expected output:
*****
****
***
**
*

In terms of the equivalences between for loops and while loops, it's basically this: for循环和while循环之间的等效而言, 基本上是这样的:

for (INIT; CONDITION; POSTOP) {     INIT;
    BODY;                           while (CONDITION) {
}                                       BODY;
                                        POSTOP;
                                    }

(with variations for scope and other such things that we don't need to go into here). (具有范围和其他不需要我们介绍的类似内容的变体)。

Hence, to solve your problem with a for/while solution, you could use something like: 因此,要使用for/while解决方案解决问题,可以使用类似以下内容的方法:

for (int i = 0; i < 5; i++) {
    int j = i;
    while (j < 5) {
        System.out.print("*");
        j++;
    }
    System.out.println();
}

It's sometimes helpful to run through the code in your head, with a pen and a bit of paper to maintain variables, such as: 有时用钢笔和一些纸来维护变量,例如遍历您头脑中的代码,这很有帮助:

 i   j   output
--- ---  ------

If you just "execute" each line of the code (either your original or my for/while variant) in your head for a few iterations, you should see what's happening. 如果您只是在脑海中“执行”代码的每一行(无论是原始代码还是for/while变体)几次,您都应该看到正在发生的事情。 And, if you do them side-by-side, you'll see the equivalency between both variants. 而且,如果您并排执行它们,将会看到两个变体之间的等效性。

Basically, the outer loop counts (iterates) from 0 to 4 inclusive, running the inner loop then outputting a newline character. 基本上,外循环从0到4(含)计数(迭代),运行内循环然后输出换行符。

For each of those iterations, the inner loop counts from i to 4 inclusive, outputting a * each time (with no newline character). 对于这些迭代中的每一个 ,内部循环的计数从i到4(含4),每次输出一个* (不带换行符)。

So, in the first outer loop iteration, the inner loop runs from 0 to 4 , outputting five stars. 因此,在第一次外循环迭代中,内循环从04运行,输出五颗星。

In the second outer loop iteration, the inner loop runs from 1 to 4 , outputting four stars. 在第二个外循环迭代中,内循环从14运行,输出四颗星。

And so on, to the final outer loop iteration where i is 4 , so the inner loop runs from 4 to 4 , outputting one star. 依此类推,直到最后一次外循环迭代,其中i4 ,因此内循环从44运行,输出一颗星。

In terms of the pen-and-paper method, you would get something along the following lines: 就笔和纸方法而言,您将获得以下几方面的好处:

 i   j   output
--- ---  ------
 0   0     *
 0   1     *
 0   2     *
 0   3     *
 0   4     *
           \n
 1   1     *
 1   2     *
 1   3     *
 1   4     *
           \n
 2   2     *
 2   3     *
 2   4     *
           \n
 3   3     *
 3   4     *
           \n
 4   4     *
           \n

Well, first your for loop(s) could be written like 好吧,首先您的for循环可以写成

for (int i = 0; i < 5; i++) {
    for (int j = i; j < 5; j++) {
        System.out.print("*");
    }
    System.out.println();
}

Next, let's look at the three parts of a for loop (from the Wikipedia link) 接下来,让我们看一下for循环的三个部分(来自Wikipedia链接)

for(INITIALIZATION; CONDITION; INCREMENT/DECREMENT){
    // Code for the for loop's body
    // goes here.
}

We can move that to a while loop like 我们可以将其移动到while循环,例如

INITIALIZATION;
while (CONDITION) {
    // Code for the while loop's body
    // goes here.
    INCREMENT/DECREMENT;
}

As a single practical example, 作为一个实际的例子,

int j = i;
while (j < 5) {
    System.out.print("*");
    j++;
}

A while loop inside a for loop can be made to behave like a nested for loop if you simply update the counter variable within the body of the while loop. 如果仅在while循环主体内更新counter变量,则for循环内的while循环可以像嵌套的for循环一样工作。 Alternatively, think about what a for loop is really saying per iteration. 或者,考虑一下for循环在每次迭代中真正在说什么。

for (int i = 0; i<5;i++)
{
    int j = i;
    // for (int j=i;j<5;j++)    
    while(j < 5)
    {
       System.out.print("*");
       j++;
    }
    System.out.println();
}

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

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