-
Stars program
I am taking computer science at school and our home work this weekend was to finish a program we have been working on the past week which was to program something that would print "*" in different patterns I am trying to finish the last one and have not been able to figure out an equation that will work. I need to print a diamond. so far I have gotten the top part to print by doing the following
public class stars
{
public static void main (String[]args)
{
final int MAX_ROWS=10;
for (int row=1; row<=(MAX_ROWS-5); row++)
{
for (int star=1; star<=((MAX_ROWS-5)-row); star++)
System.out.print(' ');
for (int star=1; star<=((row*2)-1); star++)
System.out.print("*");
System.out.println();
}
}
}
I have tried invertinf the equation and changing the number star is equal to along with row and if I am adding or subtracting a star. Any advice on how to get the bottom to print would be apprieciated.
-
Re: Stars program
Have you considered using spaces instead of stars, and using stars where you have used spaces for the bottom part?
-
Re: Stars program
So isn't a diamond just a centered collection of rows that contain first a growing number of * and then a diminishing number of stars?
So, we can assume several things from this. We're doing something over and over, so: Loops
You got that far.
Also, we're doing two things over and over, rows, and columns. This implies a nested loop (a loop in a loop).
Finally, we have two behaviors - a growing number of stars, followed by a shrinking number of stars. One way to do this is two sets of two loops, one that grows, and one that shrinks.