Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Diamond perimeter with loop

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Diamond perimeter with loop

    Hi. I've beed trying to do this for days but I can't.



    I got the program to do the whole diamond but I don't know how to take out all the asterisk. żAny idea?

    This is my code:

    public class diamond
    {
    	public static void main(String args[])
    	{
    		int i=0,j,k,n=4;
    		for(k=1;k<=n;k++) {
    			for(i=0;i<=n-k;i++) {
    				System.out.print(" ");
    			}
    			for(j=1;j<=k;j++) {
    				System.out.print("*");
    			}
    			for(j=1;j<=k-1;j++) {
    				System.out.print("*");
    			}
    			System.out.println("");
    		}
    		for(k=1;k<=n;k++) {
    			for(i=0;i<=k;i++) {
    				System.out.print(" ");
    			}
    			for(i=1;i<=n-k;i++) {
    				System.out.print("*");
    			}
    			for(j=0;j<=n-k-2;j++) {
    				System.out.print("*");
    			}
    			System.out.print("\n");
    		}
    	}
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Diamond perimeter with loop

    Apart from the first and last line, each line has: N spaces, star, M spaces, star. All you have to do is work out what N and M is for each line.
    Improving the world one idiot at a time!

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Diamond perimeter with loop

    Inside the outer loop, keep a flag, which will keep track if the very first * has been printed, don't let the program draw * again, unless it's the last index of inner loop.
    Note: Don't forget to reset the flag.

  4. #4
    Junior Member
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Diamond perimeter with loop

    Quote Originally Posted by Mr.777 View Post
    Inside the outer loop, keep a flag, which will keep track if the very first * has been printed, don't let the program draw * again, unless it's the last index of inner loop.
    Note: Don't forget to reset the flag.
    Your refering about IF and else statement right?

  5. #5
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Diamond perimeter with loop

    yes that is what he is saying

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Diamond perimeter with loop

    Quote Originally Posted by Rage1337 View Post
    Your refering about IF and else statement right?
    Yes, sorry for not making it clear.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

Similar Threads

  1. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  2. diamond pattern and loop
    By Naif in forum Loops & Control Statements
    Replies: 4
    Last Post: October 31st, 2011, 10:51 PM
  3. Method to print a diamond
    By snowMac in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 8th, 2011, 04:18 AM
  4. asterisk forming diamond
    By cutee_eyeh in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 13th, 2010, 08:53 PM
  5. Is it possible to print a spade, club, heart, or diamond in java?
    By BC2210 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 2nd, 2009, 08:35 PM