代码1:我已经int b = 0
在第一个while循环之外声明了:
public class apples {
public static void main(String args[]){
int a = 1;
int b = 0;
while (a < 5){
while(b < a){
System.out.print("y");
b++;
}
System.out.println();
a++;
}
}
}
结果 :
y
y
y
y
代码2:在这里,我int b = 0
在第一个while循环内声明:
public class apples {
public static void main(String args[]){
int a = 1;
while (a < 5){
int b = 0;
while(b < a){
System.out.print("y");
b++;
}
System.out.println();
a++;
}
}
}
结果 :
y
yy
yyy
yyyy
如您所见,当我int b = 0
在第一个while'循环内和该循环外声明时,我得到不同的结果。谁能解释为什么会这样?已经谢谢你了!
在这种情况下,足够的System.out.println
s永远是不够的,您可以通过简单地编写以下内容来学到很多东西:
int a = 1;
int b = 0;
while (a < 5) {
System.out.println("Outer loop: a="+a+", b="+b);
while (b < a) {
System.out.println("Inner loop: a="+a+", b="+b);
b++;
}
System.out.println();
a++;
}
玩游戏,更改初始值,在内循环之后/之前重置值,这是学习某些东西的最佳方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句