Reputation: 141
I've been struggling with this for loop pattern in Java(with spaces on both sides)
0
000
00000
0000000
00000
000
0
Heres what I have been doing:
for (int i = 1; i <= 5; i++) {
for (int s = 5; s > i; s--) {
System.out.print(" ");
}
for (int j = 1; j < i; j++) {
System.out.print("0");
}
for (int j = 1; j < i; j++) {
System.out.print("0");
}
System.out.println("");
}
for (int i = 1; i <= 5; i++) {
for (int s = 1; s < i; s++) {
System.out.print(" ");
}
for (int j = 5; j > i; j--) {
System.out.print("0");
}
for (int j = 5; j > i; j--) {
System.out.print("0");
}
System.out.println("");
}
As you can probably tell, I've been able to figure out how to print this pattern using an odd number of 0's in each row, but I can't for the life of me figure out how to do it with odd numbers. Thanks.
Upvotes: 0
Views: 1212
Reputation: 40396
Sometimes it helps to parameterize the problem a bit; then you can write it down on paper and see if there are any patterns. For example, in your desired output:
0
000
00000
0000000
00000
000
0
First, let's pick a convenient way to describe a row. Let's say a row i
is s
spaces followed by z
zeros, and the whole pattern can be rows
rows long. Now write it out. When rows
is 7:
i s z
0 3 1
1 2 3
2 1 5
3 0 7
4 1 5
5 2 3
6 3 1
How about if rows
is, say, 5:
i s z
0 2 1
1 1 3
2 0 5
3 1 3
4 2 1
What about even numbers? We have to pick how we want it to look, how about:
00
0000
000000
000000
0000
00
So, when rows
is 6:
i s z
0 2 2
1 1 4
2 0 6
3 0 6
4 1 4
5 2 2
Ok, so now, let's look for patterns! z
is easy: We can see, both from the numbers and visually that z
is a function of s
and rows
:
z = rows - (2 * s);
That is, we know the total width is equal to the height (rows), and we know there are the same number of spaces before and after the zeroes.
So now it boils down to figuring out s
from i
and rows
. Well, after a bit of head scratching and experimenting, we can see that when i < rows / 2
:
s = (rows - 1) / 2 - i;
And when i >= rows / 2
:
s = i - rows / 2;
And now, it all comes together, e.g.:
void diamond (int rows) {
for (int i = 0; i < rows; ++ i) {
int s;
if (i < rows / 2)
s = (rows - 1) / 2 - i;
else
s = i - rows / 2;
int z = rows - (2 * s);
// print s spaces, then z zeroes, then a newline
}
}
I'll leave the task of printing s
spaces and z
zeroes as an exercise to the reader.
This is a fairly general problem solving technique for this type of problem:
Step 1 is the most important step, as it defines a way to convert your complex-looking task (drawing a diamond) into a much simpler problem (determining s
as a function of i
and rows
).
Upvotes: 2
Reputation: 1874
try this,
public static void main(String []args){
int i = -1;
for (int j = 0; j < 7; j +=2) {
i++;
for (int k = i; k < 3; k++)
System.out.print(" ");
for (int z = 0; z <= j; z++) {
System.out.print(0);
}
System.out.println();
}
i=0;
for(int j=4; j>=0;j-=2){
i++;
for(int k=0;k<i;k++)
System.out.print(" ");
for(int z=0;z<=j ;z++)
System.out.print(0);
System.out.println();
}
}
Upvotes: 1
Reputation: 1485
you can try this -
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");
}
}
Upvotes: 1
Reputation: 79877
How about just changing the first j=1
to j=2
and changing the first j=5
to j=4
, so that you get one less zero from each of those loops?
Upvotes: 0