Results 1 to 12 of 12
  1. #1
    Jar
    Jar is offline Member
    Join Date
    Jan 2014
    Posts
    5
    Rep Power
    0

    Default Problem with nested for loops?

    Hi , Could you please help me figure out how to solve this problem?

    Modify your code from the previous problem to produce the following output:

    99999888887777766666555554444433333222221111100000
    99999888887777766666555554444433333222221111100000
    99999888887777766666555554444433333222221111100000
    99999888887777766666555554444433333222221111100000
    99999888887777766666555554444433333222221111100000

    This is what i have but its wrong idk why ?


    Java Code:
    for(int*i*=*0;i*<=*9;i++)
    for*(int*j*=*0;j*<=5;j++)
    System.out.println(i);
    System.out.println("");

  2. #2
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    165
    Rep Power
    2

    Default Re: Problem with nested for loops?

    Well for starters if you want to nest a for loop you will need brackets. i.e:

    Java Code:
    for(int i = 0; i < 10; i++) {
        for(int j = 0; j < 10; j++) {
            // code here
        }
    }

  3. #3
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Problem with nested for loops?

    Right along after, if you want to print from high to low, start at 9 and go down to 0 in the first for loop, in the next for loop (the nested one) go from 0-5 and print the value from the first for loop.
    1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111

  4. #4
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    165
    Rep Power
    2

    Default Re: Problem with nested for loops?

    So here's what you should do.

    Java Code:
    for(int i = 9; i >= 0; i--) {
        for(int j = 0; j < 5; j++) {
            System.out.print(i);
        }
    }

  5. #5
    Jar
    Jar is offline Member
    Join Date
    Jan 2014
    Posts
    5
    Rep Power
    0

    Default Re: Problem with nested for loops?

    so i could i do something like this

    Java Code:
    for(int i = 9; i >= 0; i--) {
        for(int j = 0; j < 5; j++) {
            for(int k = 0; k <= 5; k++){
                System.out.print(i);
                    if(j==5 && k==5) 
                   System.out.println("");
            }
        }
    }

  6. #6
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    165
    Rep Power
    2

    Default Re: Problem with nested for loops?

    Why do you need 3 for loops? You do not need to k for loop because it will make you print out i 25 times.

    EDIT: What you need is the k loop to surround both the i and j for loops. And then after the j for loop you can just print out a new line to make your output on the next line.
    Last edited by pj6444; 01-23-2014 at 03:01 AM. Reason: More Details

  7. #7
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Problem with nested for loops?

    Yes, pj's solution will work
    1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111

  8. #8
    Jar
    Jar is offline Member
    Join Date
    Jan 2014
    Posts
    5
    Rep Power
    0

    Default Re: Problem with nested for loops?

    i dont uinderstand i am sorry .

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

    Default Re: Problem with nested for loops?

    As far as I can tell, PJs solution won't work - try running it, and you'll get one long line.

    You need to add this.

    Java Code:
    for(int i = 9; i >= 0; i--) {
        for(int j = 0; j < 5; j++) {
            System.out.print(i);
        }
      System.out.println("");
    }
    To explain to you what is happening:

    With just one forloop, you are printing out something everytime the forloop repeats itself.

    Aka, if you have
    Java Code:
    for(int i = 0; i < 5; i++) {
    System.out.print("-");
    }
    You'll get
    -----
    as an output.

    To make it so that you get multiple lines, you nest that forloop inside of another forloop, that'll print out that line with every iteration.

    You must remember to add a println in every iteration of the 'line' forloop - basically like pressing "enter" when you're typing a word document

  10. #10
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Problem with nested for loops?

    Quote Originally Posted by AlexGraal View Post
    As far as I can tell, PJs solution won't work - try running it, and you'll get one long line.

    You need to add this.

    Java Code:
    for(int i = 9; i >= 0; i--) {
        for(int j = 0; j < 5; j++) {
            System.out.print(i);
        }
      System.out.println("");
    }
    To explain to you what is happening:

    With just one forloop, you are printing out something everytime the forloop repeats itself.

    Aka, if you have
    Java Code:
    for(int i = 0; i < 5; i++) {
    System.out.print("-");
    }
    You'll get
    -----
    as an output.

    To make it so that you get multiple lines, you nest that forloop inside of another forloop, that'll print out that line with every iteration.

    You must remember to add a println in every iteration of the 'line' forloop - basically like pressing "enter" when you're typing a word document
    I was referring to his edit, where he makes a remark about adding the 3rd for loop and changing the println position, like so:

    Java Code:
    for (int i = 0; i < 5; i++) {
                for (int j = 9; j >= 0; j--) {
                    for (int k = 0; k < 5; k++) {
                        System.out.print(j);
                    }
                }
                
                System.out.println();
            }
    EDIT: Oh god the syntax highlighter needs indenting.
    1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111

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

    Default Re: Problem with nested for loops?

    Oh my bad. Sorry, I wasn't reading carefully enough I guess!

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: Problem with nested for loops?

    Here you go!

    Java Code:
    System.out.println("99999888887777766666555554444433333222221111100000");
    System.out.println("99999888887777766666555554444433333222221111100000");
    System.out.println("99999888887777766666555554444433333222221111100000");
    System.out.println("99999888887777766666555554444433333222221111100000");
    System.out.println("99999888887777766666555554444433333222221111100000");
    You no longer have a problem with nested loops.

    Regards,
    Jim
    Last edited by jim829; 01-23-2014 at 06:55 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. n Nested Loops
    By joyful in forum New To Java
    Replies: 6
    Last Post: 10-16-2012, 05:14 AM
  2. Problem with nested for loops
    By subzero1012 in forum New To Java
    Replies: 3
    Last Post: 04-07-2012, 11:06 PM
  3. Nested Loops Help!
    By spendogw in forum New To Java
    Replies: 2
    Last Post: 03-16-2012, 05:20 AM
  4. Replies: 20
    Last Post: 03-22-2011, 05:02 AM
  5. Nested loops
    By Aestuv in forum New To Java
    Replies: 3
    Last Post: 02-11-2011, 12:40 AM

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
  •