for (int i = 0; i < 10; i++) {
System.out.println("XXXXXXXXXXOOOOOOOOO".substring(9 - i, 19 - i));
}
This uses a string of 10 Xs and 9 Os
It suggests that you could do:
for (int i = 0; i < 10; i++) {
int xs = 1 + i;
int os = 9 - i;
// xs + os == 10
... call System.out.print("X"); in a loop
... call System.out.print("O"); in a loop
System.out.println();
}
The forgotten review
You asked for a totally different approach, less tedious. So let's give a perspective of some higher abstractness.
Typically (in math for instance) you are distinghuishing cases you handle.
So you get if i == 1
and so on. That is a very static view here. In the end you listed all changes possible.
However here there is change and you can look for dynamic laws, unvarying conditions (like in physics).
That would be that the number of Xs and Os are and stay 10 and that the number of Xs increase at every i, and the number of Os decrease at every i.
Also you see something happening in two dimensions. Two nested for-loops should be the ordinary intuitive approach (not necessarily right though). The change is minimal line by line, one X/O. It is related to i
only. There is just one free variable, freedom, here, so it is a linear problem.
Now back on earth, a valid approach would be:
for i from 0 to 10
for _ from 0 to i+1 do print "X"
for _ from i+1 to 10 do print "O"
newline
or
for i from 0 to 10
for j from 0 to 10 do print if j <= i then "X" else "O";
newline
As for less tedious, more playful, that would be "XXX...OOO".substring, seeing a moving substring. Though creative, it hardly minimalistic, maybe in lines of code.