Jump to content

HELP with nested for loops! I need this output

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Pietens

Pietens

    Newbie

  • Members
  • Pip
  • 3 posts
Hello everyone,

I am brand new to Java and I am taking an introductory programming course. We have to create the following output with nested loops. I am trying but I have no idea what's going on with half of this stuff.

Here's the ouput I need:
0123456789
12345678910
234567891011
3456789101112
45678910111213
567891011121314
6789101112131415
78910111213141516
891011121314151617
9101112131415161718

Here's my disastrous code:

public class forLoopTest

{

  public static void main(String[] args)

  {

    for (int i=0; i <=9; i++)

    {

     for (int k=i; k <18; k++)

      {

        System.out.println(k);

      }

    }

    System.out.println();

  }

}

Any help would be greatly appreciated. E.g. tell me what I am doing wrong, how to fix it...

Edited by Roger, 15 February 2012 - 08:56 AM.
moved to correct forum and added code tags


#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
In your inner 'for' loop, you're using a hard coded value for the upper bound (18). If you look carefully at your target output, you'll see that each line has a constant number of outputs (10, to be precise). This means that the inner 'for' loop, the one controlling the printing of each line, must run a constant number of times. If you have a variable for your starting point (i), and a constant for your ending point (18), the number of iterations it will run for each value of 'i' will be different. What you need is to figure out how to get it to run exactly 10 times, no matter the value of 'i', yet still starting on 'i' each time. How could you write the upper bound condition on the inner loop so as to say you want it to run until the loop variable 'k' is exactly 10 more than the starting value?

The answer to that question will give you the solution to fix your code.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
Pietens

Pietens

    Newbie

  • Members
  • Pip
  • 3 posts
Hey Greg, thanks for the reply! I tried changing the upper bound to i+9 and I think it gave me the right output. Could you clarify?


for (int i=0; i <=9; i++)

    {

     for (int k=i; k <=i+9; k++)

      {

        System.out.println(k);

      }

    }

    System.out.println();

  }

}



#4
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Looks correct to me.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#5
Pietens

Pietens

    Newbie

  • Members
  • Pip
  • 3 posts
K so apparently that is incorrect. The output is supposed to be exactly what is described above (i.e. lines of code and not individual numbers on individual lines). Ugh, I don't know how you people do this!

#6
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Sorry, I overlooked one thing:

System.out.print(k);

instead of:

System.out.println(k);

Also, put this line after the inner for loop:

System.out.println();


Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users