Java 嵌套 for while 循环
Java nested for while loops
我在使用 for 循环中的嵌套 while 循环时遇到问题。我理解嵌套的 for 循环:
for (int i = 0; i<5;i++)
{
for (int j=i;j<5;j++)
{
System.out.print("*");
}
System.out.println();
}
当谈到 for 循环中的 while 循环时,我有点不知所措,有人可以向我解释一下吗?
Expected output:
*****
****
***
**
*
就for
循环和while
循环之间的等价性而言,基本上是这样的:
for (INIT; CONDITION; POSTOP) { INIT;
BODY; while (CONDITION) {
} BODY;
POSTOP;
}
(范围的变化和其他我们不需要在这里讨论的东西)。
因此,要使用 for/while
解决方案解决您的问题,您可以使用类似的方法:
for (int i = 0; i < 5; i++) {
int j = i;
while (j < 5) {
System.out.print("*");
j++;
}
System.out.println();
}
有时候用脑子里的代码运行,用笔和纸来维护变量是有帮助的,比如:
i j output
--- --- ------
如果您只是 "execute" 代码的每一行(您的原始代码或我的 for/while
变体)在您的脑海中进行几次迭代,您应该会看到发生了什么。而且,如果您并排执行它们,您会看到两种变体之间的等效性。
基本上,外循环从 0 到 4 计算(迭代),运行内循环然后输出换行符。
对于这些迭代中的 each,内循环从 i
计数到 4(含),每次输出一个 *
(没有换行符) .
所以,在第一个外循环迭代中,内循环运行s从0
到4
,输出五颗星。
第二次外循环迭代,内循环运行s从1
到4
,输出四颗星。
以此类推,直到i
为4
的最终外循环迭代,所以内循环运行从4
到4
,输出一星
就纸笔法而言,您会得到以下几行信息:
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
嗯,首先你的 for
循环可以写成
for (int i = 0; i < 5; i++) {
for (int j = i; j < 5; j++) {
System.out.print("*");
}
System.out.println();
}
接下来,我们来看一下for
loop的三个部分(来自维基百科link)
for(INITIALIZATION; CONDITION; INCREMENT/DECREMENT){
// Code for the for loop's body
// goes here.
}
我们可以将其移动到 while
循环中,例如
INITIALIZATION;
while (CONDITION) {
// Code for the while loop's body
// goes here.
INCREMENT/DECREMENT;
}
举一个实际例子,
int j = i;
while (j < 5) {
System.out.print("*");
j++;
}
如果您只更新 while 循环体内的计数器变量,则可以使 for 循环内的 while 循环表现得像嵌套 for 循环。或者,想想 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();
}
我在使用 for 循环中的嵌套 while 循环时遇到问题。我理解嵌套的 for 循环:
for (int i = 0; i<5;i++)
{
for (int j=i;j<5;j++)
{
System.out.print("*");
}
System.out.println();
}
当谈到 for 循环中的 while 循环时,我有点不知所措,有人可以向我解释一下吗?
Expected output:
*****
****
***
**
*
就for
循环和while
循环之间的等价性而言,基本上是这样的:
for (INIT; CONDITION; POSTOP) { INIT;
BODY; while (CONDITION) {
} BODY;
POSTOP;
}
(范围的变化和其他我们不需要在这里讨论的东西)。
因此,要使用 for/while
解决方案解决您的问题,您可以使用类似的方法:
for (int i = 0; i < 5; i++) {
int j = i;
while (j < 5) {
System.out.print("*");
j++;
}
System.out.println();
}
有时候用脑子里的代码运行,用笔和纸来维护变量是有帮助的,比如:
i j output
--- --- ------
如果您只是 "execute" 代码的每一行(您的原始代码或我的 for/while
变体)在您的脑海中进行几次迭代,您应该会看到发生了什么。而且,如果您并排执行它们,您会看到两种变体之间的等效性。
基本上,外循环从 0 到 4 计算(迭代),运行内循环然后输出换行符。
对于这些迭代中的 each,内循环从 i
计数到 4(含),每次输出一个 *
(没有换行符) .
所以,在第一个外循环迭代中,内循环运行s从0
到4
,输出五颗星。
第二次外循环迭代,内循环运行s从1
到4
,输出四颗星。
以此类推,直到i
为4
的最终外循环迭代,所以内循环运行从4
到4
,输出一星
就纸笔法而言,您会得到以下几行信息:
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
嗯,首先你的 for
循环可以写成
for (int i = 0; i < 5; i++) {
for (int j = i; j < 5; j++) {
System.out.print("*");
}
System.out.println();
}
接下来,我们来看一下for
loop的三个部分(来自维基百科link)
for(INITIALIZATION; CONDITION; INCREMENT/DECREMENT){
// Code for the for loop's body
// goes here.
}
我们可以将其移动到 while
循环中,例如
INITIALIZATION;
while (CONDITION) {
// Code for the while loop's body
// goes here.
INCREMENT/DECREMENT;
}
举一个实际例子,
int j = i;
while (j < 5) {
System.out.print("*");
j++;
}
如果您只更新 while 循环体内的计数器变量,则可以使 for 循环内的 while 循环表现得像嵌套 for 循环。或者,想想 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();
}