Here is what the shapes should look like :
Here is my code so far:
public class Diamonds {
public static void main(String[] args) {
for (int i = 1; i < 10; i += 2) {
for (int j = 0; j < 9 - i / 2; j++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.print("\n");
}
for (int i = 7; i > 0; i -= 2) {
for (int j = 0; j < 9 - i / 2; j++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.print("\n");
}
}
}
I am having trouble getting the second shape
By examining the shape on the right, we can notice that for each
N
asterisks on the line in the left shape, the right one has10 - N
, so, taking your code and extending it, we can get:And if we extract some common code:
You may like that :
try this :
output :
In order to not spoil your fun with this problem, I will explain what you need to do without writing any code.
To get the second shape in there, you would need to add two additional nested
for
loops into each of the two "outer" loops that you already have.Loops number three will produce a fixed number of spaces. Note that the distance between the right edge of the first shape and the left edge of the second shape is constant, so your third loops will be easy to code up.
Loops number four will loop like your first loop, but they would change places: the first inner loop from the first outer loop will be the forth inner loop in the second outer loop, and vice versa.