Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1181
  • Last Modified:

Make a diamond shape

Hi.....I am having problem on coding and make a diamond shape with 9 rows.
so, 1st row has 1 star, 2nd row has 3 star, 3rd row has 5 star etc...

I got this so far but just dunno how to continue:

             for ( int counter = 1; counter <= number; counter++ )
            {
                  for ( int side = 1; counter <= side; --side )
                        System.out.print( ' ' );
                                                
                        for ( int side = 1; side <= number; side++ )
                        
                        System.out.print( '*' );
                        System.out.println();
                  


                  }


Anyone can advise? Thank you so much! I am a beginner, so please be patient~
0
civicy2k
Asked:
civicy2k
  • 2
  • 2
1 Solution
 
fave_17Commented:
Hi!

I think this one is a homework.

Here are some tips for creating a diamond shape.
1. Create a loop for the upper half of the diamond. And then another one for the lower half.
2. The number of "*" for every row increments by 2.
3. The number of " " for the first row =  # of rows / 2 - 1 (The 1 is for the asterisk). Then for the upper half, it will decrement by 1 for the succeeding rows .
For the lower half, it will increment by 1.

I hope I helped.

fave_17
0
 
objectsCommented:
easier if you do the top half 1st, then the bottom half, try something like:

for (int i=1; i<number; i+=2)
{
   for (int j=0; j<(number-i)/2; j++) System.out.print(' ');
   for (int j=0; j<number; j++) System.out.print('*');
   System.out.println();
}
for (int i=number; j>0; i-=2)
{
   for (int j=0; j<(number-i)/2; j++) System.out.print(' ');
   for (int j=0; j<number; j++) System.out.print('*');
   System.out.println();
}

0
 
civicy2kAuthor Commented:
When I modified mine to:
             for ( int counter = 1; counter <= number; counter+=2 )
            {
                  for ( int side = 0; side <= (number-counter)/2; side++ )
                  System.out.print( ' ' );
                                                
                        for ( int side = 0; side <= number; side++ )
                        System.out.print( '*' );
                        System.out.println();
                  }

            System.out.println();
            
            for ( int counter = number; counter >0; counter-=2 )
            {
                  for ( int side = 0; side < (number-counter)/2; side++ )
                  System.out.print( ' ' );
                  
                        for ( int side = 0; side < number; side++ )
                        System.out.print( '*' );
                        System.out.println();
                  }

It printed out...........

     **********
    **********
   **********
  **********
 **********

*********
 *********
  *********
   *********
    *********

how come? Thanks
0
 
objectsCommented:
>  for (int j=0; j<number; j++) System.out.print('*');

woops, should be drawing i stars, not number. that should be

 for (int j=0; j<i; j++) System.out.print('*');
0
 
civicy2kAuthor Commented:
objects,
thanks a lot, that helps me so much!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now