I'm putting together a code to output the following pattern:
000000000X
00000000XX
0000000XXX
000000XXXX
00000XXXXX
0000XXXXXX
000XXXXXXX
00XXXXXXXX
0XXXXXXXXX
(each line should be one after another. I'm not quite sure how to display the pattern on forums...sorry)
I'm supposed to use a recursive loop inside the code but I end up in an infinite loop and I really don't understand why..(It may be ok to assue that I've never used a recursive loop practically).This is my code:
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);
}
}