I wrote a code that will print a diamond
static void printDiamond(int size) {
for (int i = 0; i < size; i++) {
for (int a = 0; a < (size - (i + 1)); a++) {
System.out.print(" ");
}
System.out.print("X");
for (int b = 0; b < (i * 2); b++) {
System.out.print(" ");
}
System.out.print("X");
System.out.println();
}
for (int i = size-1; i >= 0; i--) {
for (int a = 0; a < (size - (i + 1)); a++) {
System.out.print(" ");
}
System.out.print("X");
for (int b = 0; b < (i * 2); b++) {
System.out.print(" ");
}
System.out.print("X");
System.out.println();
}
}
The problem that I encounter with a diamond is that it will print double for all that I enter. So, if the user had to enter 6 for diamond, he should look like this:
XX
X X
X X
X X
X X
XX
With my code, if the user enters 5, it prints the following data:
XX
X X
X X
X X
X X
X X
X X
X X
X X
XX
Instead of printing 5 lines, it prints 10. If I enter 3, it will print 6 lines instead of 3. It seems that for my diamond it displays the number that it receives from the user, but then prints this number of times 2. There is is there a way so that I can reduce the size of the method printDiamondso that it has the correct number of rows?