When I finished this problem, I thought about how I initialized my loop iterator variables i
and j
. Originally I had i = 0
and j = 0
, but then I had to initialize count
to 1
, instead of 0
. And that just seemed a bit odd to me. A similar conundrum came to me when I was incrementing/decrementing count
, and deciding if I wanted to take the sum with + count
, or the difference with - count
. I started by using count++
, and it seems like I did so out of habit. So, what do you prefer when you're making these sorts of decisions? Is there a general guideline for this sort of thing? Something that'd make the decisions easier for me to make? Any feedback is appreciated.
Write a static method named xo that accepts an integer size as a parameter and prints a square of size by size characters, where all characters are "o" except that an "x" pattern of "x" characters has been drawn from the corners of the square. In other words, on the first line, the first and last characters are "x"; on the second line, the second and second-from-last characters are "x"; and so on. If 0 or less is passed for the size, no output should be produced.
public static void xo(int size) {
if (size > 1) {
int count = 0;
for(int i = 1; i <= size; i++) {
for(int j = 1; j <= size; j++) {
if((i == j) || (size - j - count == 0)) {
System.out.print("x");
} else {
System.out.print("o");
}
}
count++;
System.out.println();
}
} else if(size == 0) {
} else if(size == 1) {
System.out.println("x");
}
}
else if(size == 0) { }
empty? \$\endgroup\$