【发布时间】:2014-10-13 07:28:03
【问题描述】:
我正在整理一个代码以输出以下模式:
000000000X
00000000XX
0000000XXX
000000XXXX
00000XXXXX
0000XXXXXX
000XXXXXXX
00XXXXXXXX
0XXXXXXXXX
(每一行应该是一个接一个。我不太清楚如何在论坛上显示模式......对不起)
我应该在代码中使用递归循环,但我最终陷入了无限循环,我真的不明白为什么..(可以假设我实际上从未使用过递归循环)。这是我的代码:
class Recursion {
//recursion should stop after 9 attempts
static int stopindex = 9;
public static void main(String[] args) {
//a=number of "O"s and b=number of "X"s
int a = 9;
int b = 1;
recursion(a, b);
}
public static void recursion(int a, int b) {
//start of recursion at index 1
int startindex = 1;
//stop condition of recursion
if (startindex == stopindex)
return;
//printing of pattern
for (int i = a; i > 0; i--) {
System.out.print("O");
}
for (int j = 0; j < b; j++) {
System.out.print("X");
}
System.out.println();
--a;
++b;
++startindex;
recursion(a, b);
}
}
【问题讨论】:
标签: java recursion infinite-loop