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 2 of 2

Thread: Stuck -- Loop Pattern

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Stuck -- Loop Pattern

    I'm trying to produce this pattern, and I need help with my loop statements...

    -----1-----
    ----333----
    ---55555---
    --7777777--
    -999999999-

    public static void main(String[] args)
        { 
            for(int j = 1; j<=9; j=j+2)
            {
                for(int d=5;d>=1;d--)
                {
                    System.out.print("-");
                }
                for(int k=1; k<=j; k++)
                {
                    System.out.print(j); 
                }
                for(int d=5;d>=1;d--)
                {
                    System.out.print("-");
                }
                System.out.println();
            }        
        }

    My Output so far:
     
    -----1-----
    -----333-----
    -----55555-----
    -----7777777-----
    -----999999999-----


  2. #2
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Stuck -- Loop Pattern

    Well the problem is here
      for(int d=5;d>=1;d--)
    It's alway going to run 5 times. I see what your trying to do. yOu could if you were feeling clever write an algorithm so d is set to a value depending on j but you could just have a variable say count declared and set to 5 outside the loop and at the end of the loop decrease it by 1 and each time through set d=count.

  3. The Following User Says Thank You to Faz For This Useful Post:

    CodeNewb (June 11th, 2010)

Similar Threads

  1. Urgent need a java regex pattern
    By mallikarjun_sg in forum Java Theory & Questions
    Replies: 1
    Last Post: May 6th, 2010, 05:51 AM
  2. [SOLVED] Need Regex for a pattern
    By mallikarjun_sg in forum Java Theory & Questions
    Replies: 7
    Last Post: May 5th, 2010, 02:06 AM
  3. Stuck :(
    By hing09 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 17th, 2010, 05:20 PM
  4. url pattern issue - please help me...
    By bharathik in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: November 9th, 2009, 04:28 AM
  5. I'm stuck
    By iank in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 5th, 2009, 10:21 AM