-
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
-
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.
-
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);
}
}
}
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks