Results 1 to 7 of 7

Thread: For Loop

  1. #1
    YiBoog is offline Member
    Join Date
    Nov 2009
    Location
    City of Angels
    Posts
    7
    Rep Power
    0

    Default 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!!

    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();
    		}
    	}
    This is being called from the main()...
    Last edited by Eranga; 11-11-2009 at 07:16 AM. Reason: incorrect code tags are changed

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Your spaces for the top part:
    Java Code:
    			// print spaces for top
    			for (int j = 1; j <= len - i; j++)
    Your spaces for the bottom part:
    Java Code:
    			// print spaces for bottom
    			for (int j = len - 1; j <= len - i + 1; j++)
    Why don't you use equal loops for both parts? Your loop for the bottom part is incorrect (look closely).

    kind regards,

    Jos

  3. #3
    YiBoog is offline Member
    Join Date
    Nov 2009
    Location
    City of Angels
    Posts
    7
    Rep Power
    0

    Default 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.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by YiBoog View Post
    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:
    Java Code:
    			// print asterisks
    			for (int j = 1; j <= 2 * i - 1; j++)
    			{
    				System.out.print("*");
    			}
    			System.out.println();
    		}
    	}
    Are you sure? For i ==1 the last loop should print one single star ...

    kind regards,

    Jos

  5. #5
    YiBoog is offline Member
    Join Date
    Nov 2009
    Location
    City of Angels
    Posts
    7
    Rep Power
    0

    Default 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:

  6. #6
    YiBoog is offline Member
    Join Date
    Nov 2009
    Location
    City of Angels
    Posts
    7
    Rep Power
    0

    Default 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

    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
    Very respectfully,

    Boog

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by YiBoog View Post
    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
    You are welcome of course but the code you posted was not the code you were staring at on your computer (see above).

    kind regards,

    Jos

Similar Threads

  1. for loop help
    By soc86 in forum New To Java
    Replies: 9
    Last Post: 01-24-2011, 09:45 PM
  2. Loop Help!
    By Keno777 in forum New To Java
    Replies: 3
    Last Post: 11-04-2009, 01:11 AM
  3. Which loop to use?
    By meee in forum New To Java
    Replies: 1
    Last Post: 09-07-2009, 09:36 AM
  4. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM
  5. can you help me with this for loop?
    By java_fun2007 in forum New To Java
    Replies: 6
    Last Post: 12-22-2007, 10:20 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •