Results 1 to 12 of 12

Thread: i need help

  1. #1
    JAM1996 is offline Member
    Join Date
    Sep 2012
    Location
    Utah
    Posts
    7
    Rep Power
    0

    Default i need help

    Write a program to output the intergers 1, 2, 3,4,…25, five to a line via nested loops.
    Sample Output:

    1 2 3 4 5
    6 7 8 9 10
    11 12 13 14 15
    16 17 18 19 20
    21 22 23 24 25

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: i need help

    ok, and where exactly do you need help?how is your approach?

  3. #3
    JAM1996 is offline Member
    Join Date
    Sep 2012
    Location
    Utah
    Posts
    7
    Rep Power
    0

    Default Re: i need help

    writing it all

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: i need help

    brazenly....

    Java Code:
    loop from i=1 to 25
       print i
       if i modulo 5 equal 0 then print linebreak

    that`s all....

    /edit: oh i see...with nested loops..ok but it`s similar :)
    Last edited by eRaaaa; 10-14-2012 at 11:09 AM.

  5. #5
    JAM1996 is offline Member
    Join Date
    Sep 2012
    Location
    Utah
    Posts
    7
    Rep Power
    0

    Default Re: i need help

    can u not give me the full code????

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: i need help

    This isn't a code writing service.

    But something appropriate might be along these lines:

    Java Code:
    HAI
    CAN HAS STDIO?
    I HAS A VAR
    IM IN YR LOOP
        UP VAR!!1
        VISIBLE VAR N " "
        IZ LEFTOVERZ VAR!!5 LIEK 0?
            YARLY
                VISIBLE "\n"
        KTHX
    IM OUTTA YR LOOP
    KTHXBYE

  7. #7
    behedwin is offline Member
    Join Date
    Oct 2012
    Posts
    26
    Rep Power
    0

    Wink Re: i need help

    ok i found this as a fun task to try and solve...
    maybe i dident understand what you where asking for... but here is what i made :)

    Java Code:
    class santa{
     
        public static void main(String[] args) {
        	for(int counter=1;counter<=5;counter++)
        		System.out.print(counter + " ");
        	System.out.println("");
        	for(int counter=6;counter<=10;counter++)
        		System.out.print(counter + " ");
        	System.out.println("");
        	for(int counter=11;counter<=15;counter++)
        		System.out.print(counter + " ");
        	System.out.println("");
        	for(int counter=16;counter<=20;counter++)
        		System.out.print(counter + " ");
        	System.out.println("");
        	for(int counter=21;counter<=25;counter++)
        		System.out.print(counter + " ");
        	System.out.println("");
        	
        	
     
        }
     }

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: i need help

    Begin classes with a capital letter (Santa) and most (right thinking) people consider it a good idea to use braces with for loops, even if they have one line bodies.

    The OP's home^H^H^H^Hexercise called for nested loops. A pair of nested loops might do it: the outer one being the line you are printing and the inner one the position you are up to within the line.

    ---

    The OP also wanted this written in JavaScript, not Java. But don't let that get in the way of your fun!

  9. #9
    behedwin is offline Member
    Join Date
    Oct 2012
    Posts
    26
    Rep Power
    0

    Default Re: i need help

    Ups, sry. Missed that :)

    I had fun learning to solve this in my way. Sry for stealing the thread hehe.

    My biggest problem was to figure out how to do a line break.
    It seems like a waste to make a System.out.println(" "); just to make a line break and then start with a new loop on next line.
    Instead i wanted to just make one loop and tell it to break line after x amount of numbers.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: i need help

    Sry for stealing the thread hehe
    That's OK - I don't think the thread is serving any other useful purpose.

    Check out System.out.println() without any argument (nothing at all within the parentheses).

    i wanted to just make one loop and tell it to break line after x amount of numbers
    That's what I did with the LOLCODE in #7. Do you know about the remainder operator (%) because it comes in handy here.

    [Edit] I've just noticed that the loop in #7 never actually ends...
    Last edited by pbrockway2; 10-16-2012 at 11:53 PM.

  11. #11
    behedwin is offline Member
    Join Date
    Oct 2012
    Posts
    26
    Rep Power
    0

    Default Re: i need help

    ive seen % before... but seen it being used as a math value like *, / or + and -.
    not sure how it would help here?

    im probably missing something major here...
    but i had this idea that i could take

    public static void main(String[] args) {
    for(int counter=1;counter<=25;counter++)
    System.out.print(counter + " ");

    and just ad somewhere that i want java to print out the sequence 1-25
    and make a line break every 5 value or something.

    instead of having to write all that code just becouse i want it to start at a new line

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: i need help

    make a line break every 5 value
    The % operator evaluates the remainder when a number is divided. (that's what the link was supposed to explain). So 3%5=3, 17%5=2 and - importantly - 5%5=0, 10%5=0, 15%5=0 etc. So it looks very much like you should be checking:

    Java Code:
    if(counter % 5 == 0) {
        // new line goes here
    }

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
  •