Results 1 to 4 of 4
  1. #1
    Yoana is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default nested for loop!

    im trying to print this figure !!! Is there anyone that can help me in how to get the missing lines in the right of the triangle? i went to youtube and some other websites, but i wasn't able to find one that shows how to do those lines in the right side of the triangle!!
    -----1-----
    ----333----
    ---55555---
    --7777777--
    -999999999-

    but i keep getting this !

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

    this is what i have so far!!!

    public class Example {
    public static void main (String[] args){

    for (int i=1; i<=9; i=i+2)
    {

    for (int k=0; k < (5- i / 2); k++)
    {
    System.out.print("-");
    }


    for (int j=0; j<i; j++)
    {
    System.out.print(i+"");

    }

    System.out.println("");

    }


    }//end of main


    }//end of class

  2. #2
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: nested for loop!

    It'd be much easier for us to read if you put it in a code box.

    I don't see why you couldn't just add another forloop after you print the numbers that is the same exact thing as your first forloop that prints the dashes. You print out the same number of dashes on both sides...

  3. #3
    Yoana is offline Member
    Join Date
    Oct 2013
    Posts
    10
    Rep Power
    0

    Default Re: nested for loop!

    I didn't know that you could put them in a box code , sorry im new , i will next time!!!
    and thank you so much for you help i added another for loop and it works perfectly !!!

  4. #4
    eddy20 is offline Member
    Join Date
    Jan 2014
    Posts
    9
    Rep Power
    0

    Default Re: nested for loop!

    alright there is an easy way to solve this.. just use your second for loop
    1) copy your 2nd for loop

    for (int k=0; k < (5- i / 2); k++)
    {
    System.out.print("-");
    }

    2)paste it after the third for loop but before the system.out.println
    3) Run it

Similar Threads

  1. how to use nested for loop
    By geekgeek1 in forum New To Java
    Replies: 5
    Last Post: 02-04-2014, 06:16 AM
  2. Nested loop
    By Shasool in forum New To Java
    Replies: 2
    Last Post: 10-23-2011, 05:10 PM
  3. Nested Loop
    By sehudson in forum New To Java
    Replies: 2
    Last Post: 03-11-2011, 03:39 AM
  4. can some one help me with nested loop?
    By keycoffee in forum New To Java
    Replies: 10
    Last Post: 01-25-2010, 02:49 AM
  5. Nested For Loop
    By yuchuang in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 01:11 PM

Posting Permissions

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