Jump to content


Nested For Loops


  • Please log in to reply
3 replies to this topic

#1 firedude20123

firedude20123

    Newbie

  • Members
  • Pip
  • 3 posts
  • Gender:Male
  • Location:westborough MA

Posted 06 January 2011 - 06:16 PM

public class nestedforloops
{

    public static void main(String[] args)
    {

     for(int i = 0; i <5;i ++ )
     {
     System.out.print("1");
     for(int b = 1;b < 5; b++)
     {
        System.out.print(b);
     }
     System.out.println();
     }

    }
}



/*help plz


right now it prints.....

11234
11234
11234
11234
11234

however i need to print....

1
12
123
1234
12345

but i cant figure out how to make it do this with just 2 loops

Press any key to continue...



*/
CODE
public class nestedforloops
{

    public static void main(String[] args)
    {

     for(int i = 0; i <5;i ++ )
     {
     System.out.print("1");
     for(int b = 1;b < 5; b++)
     {
        System.out.print(b);
     }
     System.out.println();
     }

    }
}


#2 firedude20123

firedude20123

    Newbie

  • Members
  • Pip
  • 3 posts
  • Gender:Male
  • Location:westborough MA

Posted 06 January 2011 - 06:17 PM

i do know why it is printing the 12345 5 times so i just neeed help getting it to print the numbers like 23 234 2345  

thx guys and gals

#3 firedude20123

firedude20123

    Newbie

  • Members
  • Pip
  • 3 posts
  • Gender:Male
  • Location:westborough MA

Posted 07 January 2011 - 06:06 PM

ok i figured this one out finnaly

#4 Kraicheck

Kraicheck

    Advanced Member

  • Members
  • PipPipPip
  • 884 posts
  • Gender:Male
  • Location:Belgium

Posted 08 January 2011 - 02:25 PM

Great. Doesn't it feel good to find the solution yourself instead of it being handed to you?