?
Solved

Make a diamond shape

Posted on 2005-02-24
5
Medium Priority
?
1,177 Views
Last Modified: 2008-01-09
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
Comment
Question by:civicy2k
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:fave_17
ID: 13400534
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
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
ID: 13400535
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
 

Author Comment

by:civicy2k
ID: 13400616
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
 
LVL 92

Expert Comment

by:objects
ID: 13400639
>  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
 

Author Comment

by:civicy2k
ID: 13400669
objects,
thanks a lot, that helps me so much!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question