Results 1 to 7 of 7
Thread: For Loop
- 11-11-2009, 07:14 AM #1
Member
- Join Date
- Nov 2009
- Location
- City of Angels
- Posts
- 7
- Rep Power
- 0
For Loop
Greetings...I am working on drawing a d<>amond using my for loops. I have the top portion of my diamond working correctly but the bottom doesn't want to follow orders. The bottom triangle spaces do not match right. I was wondering if someone could take a look at where the error is coming from in the bottom triangle. I know there are more ways to do this and would also appreciate if you have other suggestions on to shorten and make this code look more elegant. Thanks!!
This is being called from the main()...Java Code:public class Diamond { static void diamondOfAsterisks(int len) { for (int i = 1; i <= len; i++) { // print spaces for top for (int j = 1; j <= len - i; j++) { System.out.print(" "); } // print asterisks for (int j = 1; j <= 2 * i - 1; j++) { System.out.print("*"); } System.out.println(); } // for each line of the bottom for (int i = len - 1; i >= 1; i--) { // print spaces for bottom for (int j = len - 1; j <= len - i + 1; j++) { System.out.print(" "); } // print asterisks for (int j = 1; j <= 2 * i - 1; j++) { System.out.print("*"); } System.out.println(); } }Last edited by Eranga; 11-11-2009 at 07:16 AM. Reason: incorrect code tags are changed
- 11-11-2009, 07:34 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
Your spaces for the top part:
Your spaces for the bottom part:Java Code:// print spaces for top for (int j = 1; j <= len - i; j++)
Why don't you use equal loops for both parts? Your loop for the bottom part is incorrect (look closely).Java Code:// print spaces for bottom for (int j = len - 1; j <= len - i + 1; j++)
kind regards,
Jos
- 11-11-2009, 09:49 AM #3
Member
- Join Date
- Nov 2009
- Location
- City of Angels
- Posts
- 7
- Rep Power
- 0
For Loop
Ok, so I made the change, however, the last star (the single one at the bottom) does not print. Is it suppose to? It leaves only 3 asterisks at the end and I thought it should have printed the last single one??? Any thoughts on this?? :confused:
Respectfully,
Boog
Java Code:public class Diamond { static void diamondOfAsterisks(int len) { for (int i = 1; i <= len; i++) { // print spaces for top for (int j = 1; j <= len - i; j++) { System.out.print(" "); } // print asterisks for (int j = 1; j <= 2 * i - 1; j++) { System.out.print("*"); } System.out.println(); } // for each line of the bottom for (int i = len - 1; i >= 1; i--) { // print spaces for bottom for (int j = 1; j <= len - i; j++) { System.out.print(" "); } // print asterisks for (int j = 1; j <= 2 * i - 1; j++) { System.out.print("*"); } System.out.println(); } }Last edited by YiBoog; 11-11-2009 at 09:56 AM.
- 11-11-2009, 10:23 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
- 11-11-2009, 07:32 PM #5
Member
- Join Date
- Nov 2009
- Location
- City of Angels
- Posts
- 7
- Rep Power
- 0
For Loop
Yes...I just ran it several times with the same result. The last single asterisk at the end does not print. I don't know where else the problem could be??:eek:
- 11-11-2009, 07:40 PM #6
Member
- Join Date
- Nov 2009
- Location
- City of Angels
- Posts
- 7
- Rep Power
- 0
For Loop
Nevermind~ I found the problem. It was in the beginning FOR loop of the bottom triangle that was giving fits for each line. I converted > to >= and it printed the last star. Wooohoooo, Thank you again for your assistance!!! :D
Very respectfully,Java Code:// for each line of the bottom for (int i = len - 1; i >= 1; i--) //instead of > I changed to >= { // print spaces for bottom
Boog
- 11-11-2009, 07:53 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
for loop help
By soc86 in forum New To JavaReplies: 9Last Post: 01-24-2011, 09:45 PM -
Loop Help!
By Keno777 in forum New To JavaReplies: 3Last Post: 11-04-2009, 01:11 AM -
Which loop to use?
By meee in forum New To JavaReplies: 1Last Post: 09-07-2009, 09:36 AM -
while-loop stopping on first loop
By davester in forum New To JavaReplies: 6Last Post: 06-26-2009, 08:46 PM -
can you help me with this for loop?
By java_fun2007 in forum New To JavaReplies: 6Last Post: 12-22-2007, 10:20 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks