|
查看: 1553|回复: 4
|
帮帮忙!!!java nested loop
[复制链接]
|
|
|
本帖最后由 reds8 于 21-7-2010 07:06 PM 编辑
java如何用nested loop来拿到这个output:
1
2 3 4
5 6 7 8 9
10 11 12 13 14 15 16
要用while/do-while/for statement |
|
|
|
|
|
|
|
|
|
|
发表于 21-7-2010 12:42 PM
|
显示全部楼层
本帖最后由 MaokeJackson 于 21-7-2010 12:43 PM 编辑
- String[] array = {
- " 1 ",
- " 2 3 4 ",
- " 5 6 7 8 9 ",
- "10 11 12 13 14 15 16",
- };
- for (int i = 0; i < array.length; i++)
- {
- System.out.println(array[i]);
- }
复制代码 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 21-7-2010 12:53 PM
|
显示全部楼层
本帖最后由 reds8 于 21-7-2010 12:55 PM 编辑
MaokeJackson 发表于 21-7-2010 12:42 PM 
谢啦!!
可是只有这个方法吗?
因为string array还有length我好像还没有学到 |
|
|
|
|
|
|
|
|
|
|
发表于 21-7-2010 08:58 PM
|
显示全部楼层
本帖最后由 宅男-兜着走 于 21-7-2010 08:59 PM 编辑
回复 3# reds8
- int count = 1; int nextLoopCount= 0; String spacing = " ";
- for(int i=1; i<= 4; i++) { System.out.print(spacing.substring(i)); for(int j=0; j <= nextLoopCount; j++) { System.out.print(count++ + " "); } System.out.print("\n"); nextLoopCount+=2;
- }
复制代码
还有这个方法 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 21-7-2010 09:46 PM
|
显示全部楼层
回复 reds8
还有这个方法
宅男-兜着走 发表于 21-7-2010 08:58 PM 
谢啦!! |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|