new programmer needs a bit of help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: new programmer needs a bit of help

  1. #1
    Join Date
    Nov 2004
    Posts
    2

    new programmer needs a bit of help

    I have to modify the program

    public class stars

    {
    public static void main ( String [] args)
    {
    final int maxrows = 10;
    for (int row = 1; row <= maxrows; row++)
    {
    for (int star = 1; star <= row; star++)
    System.out.print("*");
    System.out.println();
    }
    }
    }


    Which displays

    *
    **
    ***
    ... (for 10 rows)



    to display a diamond:

    *
    ***
    *****
    ******* <-- This should be a diamond, but for
    ********* some reason its not beingdisplayed
    *********
    *******
    *****
    ***
    *


    all with using for loops (or for the most part).
    I am frustrated and it seems like my mind clouds over when i try to envision how this should work, can someone please do me a favor and help me
    Thanks

  2. #2
    Join Date
    Sep 2004
    Posts
    150
    I'll give you a hint: You need some spaces in that bad boy. Spaces before each line of stars is made.

    Thing is, the number of spaces vary according to how many stars there are. It almost fits a pattern..


    So the code you are given produces this:

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

    To make the upper half of a diamond:

    On the first line you need 9 spaces, the second line you need 8 spaces, the third line you need 7 spaces.. all the way down to the 10th line where you need 0 spaces.

    It seems that for any given line you are on, you need the maxrows number (in this case 10) minus the number of stars (or just the number of that row, either way) worth of spaces.



    For the lower half:

    The problem gets started over but backwards

    You go from 10 to 1, instead of 1 to 10 in the for loop. You use --, instead of ++ (for both loops!).. (Don't forget to use >= instead of <=)

    But you still only print the maxrows number (in this case 10) minus the number of stars (or just the number of that row, either way) worth of spaces.


    edit: you're right the space stripping is terribly annoying, you'll have to imagine the upper and lower half of a diamond.

  3. #3
    Join Date
    Nov 2004
    Posts
    2
    hmm...
    Well my compiler doesn't work so i put togather some code, does this look right?

    public class Stars
    {
    public static void main (String[] args)
    {
    final int m = 9; // max stars
    for (int s=1; s<=m; s+=2)
    {
    int sp = (m-s)/2;
    printMultString(' ', sp);
    printMultString('*', s);
    System.out.println();
    }
    for (int s=m; s>=1; s-=2)
    {
    int sp = (m-s)/2;
    printMultString(' ', sp);
    printMultString('*', s);
    System.out.println();
    }
    }
    public static void printMultString(char letter, int times)
    {
    for (int i = 1; i<=times; i++)
    {
    System.out.print(letter);
    }
    }
    }

  4. #4
    Join Date
    Sep 2004
    Posts
    223
    Nice one matey, It works well...
    here is the output i got from compiling it and running it:


    Code:
        *
       ***
      *****
     *******
    *********
    *********
     *******
      *****
       ***
        *
    A kram a day keeps the doctor......guessing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center