while 循环输出不完整
我应该给出这个输出
* * * * *
* * * * *
* * * * * *
* * * * *
等等 5 次迭代 但它只显示前 2 个输出
这是我的代码
public class itiration {
public static void main( String args[]){
int counter1 = 1;
int counter2 = 1;
int counter3 = 1;
while(counter1<=5)
{
while(counter2<=5)
{
System.out.print("* ");
System.out.print(" ");
counter2++;
}
System.out.println();
while(counter3<=5)
{
System.out.print(" ");
System.out.print("* ");
counter3++;
}
System.out.println();
counter1++;
}
}
}
这不是作业
I am supposed to give this output
* * * * *
* * * * *
* * * * * *
* * * * *
so on and so forth 5 itirations
but it only shows the first 2 output
here's my code
public class itiration {
public static void main( String args[]){
int counter1 = 1;
int counter2 = 1;
int counter3 = 1;
while(counter1<=5)
{
while(counter2<=5)
{
System.out.print("* ");
System.out.print(" ");
counter2++;
}
System.out.println();
while(counter3<=5)
{
System.out.print(" ");
System.out.print("* ");
counter3++;
}
System.out.println();
counter1++;
}
}
}
this is not a homework
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过使用调试器单步调试该程序?
提示:外循环执行第一次迭代后,counter2 和 counter3 的值是多少?
Have you tried stepping through this program with a debugger?
HINT: After the outer loop executes its first iteration, what are the values of counter2 and counter3?
您需要在循环中重置
counter2
和counter3
(例如在counter1++
之后),否则它们在第一次运行后将保持在值 5循环的内部循环将不再运行。You need to reset
counter2
andcounter3
in the loop (aftercounter1++
for example), otherwise they'll stay at value 5 after the first run of the loop, and the inner loops will not run any more.您不会为主循环的每次迭代重置 counter2 和 counter3。
试试这个:
You're not resetting counter2 and counter3 for each iteration of the main loop.
Try this: