Java中for循环的错误输出
这是我使用 for
循环编写的一个简单程序
public class Test
{
public static void main (String[] args)
{
int high=10;
for( int low =0; low <=high; low++){
for (int mid=0; mid<=high; mid++)
{
System.out.print(mid);
}
System.out.println();
}
}
}
,但我希望输出看起来
0 1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10
etc...
10
像这样 0123456789100123456789100123456789100123456789100123456789100123456789100123456789100123 45678910012345678910012345678910012345678910
我做错了什么?
This is a simple program i wrote using for
loop
public class Test
{
public static void main (String[] args)
{
int high=10;
for( int low =0; low <=high; low++){
for (int mid=0; mid<=high; mid++)
{
System.out.print(mid);
}
System.out.println();
}
}
}
But I want the output to look like
0 1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10
etc...
10
Instead my output looks like this 012345678910012345678910012345678910012345678910012345678910012345678910012345678910012345678910012345678910012345678910012345678910
What am i doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您没有打印出任何空格。
编辑:
此外,每次通过内部循环时,您都会从
0
开始mid
,而不是从low
开始。You're not printing out any spaces.
edit:
In addition, you're starting
mid
at0
every time through the inner loop rather than starting it atlow
.应该
解决你的问题。
编辑:哦...好吧,还有:
and
Should fix you up.
EDIT: Oh ... well, that and:
您可能需要在 System.out 调用中连接一个空格。
You may want to concatenate a space to your System.out call.
在内循环中设置 mid = low。
Set mid = low in the inner loop.
您想要设置 mid = low,并打印空格,正如其他人指出的那样:
编辑:删除了虚假的 \n。
You want to set mid = low, and print spaces as others have noted:
Edit: removed the spurious \n.
下面的代码就可以了
Following code will do
我希望这能让你得到你想要的:
I hope this gets you what you want :
试试这个
Try this