8 Replies - 393 Views - Last Post: 06 October 2013 - 01:29 PM Rate Topic: -----

#1 rubyxleo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-September 13

Nested Loops

Posted 05 October 2013 - 12:56 PM

for(int i = 1; i <= 9; i++)	
		{
			
			for(int j = 1; j <= 9-1; j++)	//the "j=9" is the number it begins with
			{
				System.out.print("");
			}
		    {
				for(int k = 1; k <= i; k++)//the k<=5 is the number of times it prints a number
		        {
		            System.out.print(i); 		            		    
		        }
		    }
		}


This prints 122333444455555666666777777788888888999999999.
I've tried working with the 3rd and 2nd loops by changing K<=i to 5 but it's not working.
Am I even on the right track?
Can someone please explain to me how to print this 5 times?

This post has been edited by Flukeshot: 05 October 2013 - 01:29 PM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Nested Loops

#2 Flukeshot  Icon User is online

  • A little too OCD
  • member icon

Reputation: 416
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Nested Loops

Posted 05 October 2013 - 01:32 PM

You should start by cleaning up that indentation mess. You mightn't believe it, but it can help you read your code and understand it.

As for the nested loops, can you be specific about what your desired goal is?
Was This Post Helpful? 0
  • +
  • -

#3 rubyxleo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-September 13

Re: Nested Loops

Posted 05 October 2013 - 01:37 PM

Yes, my goal is to print that line 5 times.
122333444455555666666777777788888888999999999
122333444455555666666777777788888888999999999
122333444455555666666777777788888888999999999
122333444455555666666777777788888888999999999
122333444455555666666777777788888888999999999
Was This Post Helpful? 0
  • +
  • -

#4 ChrisNt  Icon User is offline

  • cute bug

Reputation: 212
  • View blog
  • Posts: 648
  • Joined: 31-July 13

Re: Nested Loops

Posted 05 October 2013 - 01:54 PM

This does nothing..
for(int j = 1; j <= 9-1; j++) {//the "j=9" is the number it begins with
    System.out.print("");
}



You need a loop before your first one that goes to the next line
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Nested Loops

Posted 05 October 2013 - 05:56 PM

System.out.print("");

does nothing
Was This Post Helpful? 0
  • +
  • -

#6 Flukeshot  Icon User is online

  • A little too OCD
  • member icon

Reputation: 416
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Nested Loops

Posted 05 October 2013 - 07:30 PM

It's a lot simpler than you seem to think it is.

Start by visualising your goal, how would you create this pattern with a pen and paper?

You need 2 loop to print 122333444455555666666777777788888888999999999\n, these should be your inner loops:

for(int j = 0; j < 10; j++) { //runs 10 times from 0 to 9
	for(int k = 0; k < j; k++) { //runs j times per iteration of j
		System.out.print(j); //the result is 1223334444...999999999
	}
}


As you can see there is no need for any complicated conditions like j <= 9-1 (which really just means j <= 8 by the way).

Then what you need to do is encase these loops in one more outer loop to ensure they run 5 times.

for(int i = 0; i < 5; i++) { //our previous set of loops will be run 5 times
	for(int j = 0; j < 10; j++) {
		for(int k = 0; k < j; k++) {
			System.out.print(j);
		}
	}
}


Lastly all this program is missing is a new line between each set of numbers.

for(int i = 0; i < 5; i++) { //our previous set of loops will be run 5 times
	for(int j = 0; j < 10; j++) {
		for(int k = 0; k < j; k++) {
			System.out.print(j);
		}
	}
	//this point is the last line of our 'i' loop, so we put it here
	System.out.println();
}


And it's done.

Programming is all about breaking large, difficult problems into small easy ones, so never over-complicate your goal, simplify it.
Was This Post Helpful? 1
  • +
  • -

#7 rubyxleo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-September 13

Re: Nested Loops

Posted 06 October 2013 - 01:13 PM

Thank you so much for your help Flukeshot. I'm new to Java and I am learning on my own. Can you give me any suggestions on the best way to go about learning it on my own? Any good resources or books I can use?
Was This Post Helpful? 0
  • +
  • -

#8 jjh08  Icon User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 198
  • Joined: 13-July 12

Re: Nested Loops

Posted 06 October 2013 - 01:21 PM

View Postrubyxleo, on 06 October 2013 - 01:13 PM, said:

Thank you so much for your help Flukeshot. I'm new to Java and I am learning on my own. Can you give me any suggestions on the best way to go about learning it on my own? Any good resources or books I can use?

You can try the Java Tutorials or read the various tutorials here on dreamincode.net

Also, I recommend either Head First Java for beginners or Thinking In Java for more in-depth information.
Hope that helps :)
Was This Post Helpful? 1
  • +
  • -

#9 Flukeshot  Icon User is online

  • A little too OCD
  • member icon

Reputation: 416
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Nested Loops

Posted 06 October 2013 - 01:29 PM

I taught myself Java with the SCJP Study Guide by Kathy Sierra and Bert Bates, but it's tailored for the OCJP Exam, which you might not necessarily be taking. I know from experience that the Head First series that jasonjh mentions is great at introductory guides.

This post has been edited by Flukeshot: 06 October 2013 - 01:30 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1