Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 making diamonds
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Prince




PostPosted: Fri Feb 06, 2004 10:59 am   Post subject: making diamonds

i need help making the top part of a diamond... the purpose is to put in an odd integer (ex: 5) and it would print out a diamond...

so if u put in 5 it would look like this:
s*****
*******
s*****
ss***
sss* (the s is a space)

the stuff under the line ive got, its the top im struggling on... any suggestions?

code:

import java.awt.*;
import hsa.Console;

public class Star
{
    static Console c;

    public static void main (String [] args)
    {
        c = new Console ();

        int size = 0;
        int space = 1;

        do
        {
            c.println ("Enter an odd number between 1 and 25: ");
            size = c.readInt ();
        }
        while (size < 1 || size > 25);

        c.println ("");

        /* while (size>=1)
         {
         for (int a=1;a<space;a++)
         */

        while (size >= 1)
        {
            for (int a = 1 ; a < space ; a++)
                c.print (" ");

            for (int b = 0 ; b < size ; b++)
                c.print ("*");

            c.println ("");
            size -= 2;
            space++;
        }

    }
}
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Fri Feb 06, 2004 3:32 pm   Post subject: (No subject)

here's a pseudocode, that sortof works:

code:

int w = 9;     // width of diamond

print    repeat " "  by  w - ((w - 2) mod w);  // might have to replace these
print    repeat "*"  by  (w - 2) * 2;          // two lines with better code
println;

for (int i = w - 1; i >= 0; i--) {
    print    repeat  " "  by   w - (i mod w);
    print    repeat  "*"  by   i * 2;
    println;
}
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: