• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with nested loops  RSS feed

 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write a nested loop that displays 10 rows of '#' characters. There should be 15 '#'characters in each row, printed using a loop that counts out fifteen characters.

One of the lines of the inner loop body should be System.out.println("#");




the nested loop structure is the part I grade. It's trivial to solve this using println("##########"); but that misses the point of the lab.
Unfortunately, I said println("#"); should be a line of code on the inner loop. The inner loop should probably be print() so multiple # characters show up on the same line.
To be extra clear: you may use println() somewhere else in the code. That's fine.
So long as you have properly nested loops, I'm not picky about what functions you use to create the output. Sorry for any confusion!




I don't know how to do this...  could someone explain how to do it and why it works to me please?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to step back, and think through the problem. That's how you learn to code. 

Your teacher has told you that you must use this line:



Do you know what that does?  if not, write a simple program that has nothing but that one line (and the basic structure needed to let that line execute.

If so, think how that can help you.  Think about what you need to do. 

another way to approach this would be to analyze the problem What do you need to do?  well, you need to print ten lines of "something".  Forget (for now) what that something is, just write a program that prints "hello" ten times (or something like that. Once you have that part working, think about what that something really needs to be...

start small, and build up your program a line or two at a time.

 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:you need to step back, and think through the problem. That's how you learn to code. 

Your teacher has told you that you must use this line:



Do you know what that does?  if not, write a simple program that has nothing but that one line (and the basic structure needed to let that line execute.

If so, think how that can help you.  Think about what you need to do. 

another way to approach this would be to analyze the problem What do you need to do?  well, you need to print ten lines of "something".  Forget (for now) what that something is, just write a program that prints "hello" ten times (or something like that. Once you have that part working, think about what that something really needs to be...

start small, and build up your program a line or two at a time.




Alright,  I see what I have to do.  This is what I have so far:




now the inside loop needs to make each line 15 # long
what I did so far made me the 10 rows I needed:

Right now the output is:
#
#
#
#
#
#
#
#
#
#


now I need to figure out how to extend them...


do I need to make another for(int y etc.){}?



 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For this assignment: yes.

To give a small hint: write line 8 as:

and note the difference in outcome.
 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:For this assignment: yes.

To give a small hint: write line 8 as:

and note the difference in outcome.


Alright,  I've been working on it and so far I have:


and now the output is:
#################
#
#
#
#
#
#
#
#
#
#





Im required to use nested loops and not one.  and now I don't know how to add on to the other #'s


needs to be 10 rows 15 characters each... ahhhh
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another hint:
You can use this to end the printing on one line and move to the next line:
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im required to use nested loops

Use the outer loop to control the number of rows
and the inner loop to control what is printed on each row.
 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norman Radder wrote:
Im required to use nested loops

Use the outer loop to control the number of rows
and the inner loop to control what is printed on each row.



like this?  I have the right number of rows.  but now only the last row has the right number of #'s hmm..


 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You apparently don't know what it means for two loops to be nested. One loop must be implemented *inside* another.
 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:You apparently don't know what it means for two loops to be nested. One loop must be implemented *inside* another.



I literally just caught that they weren't together!!!

this is getting close now!





This works.   But I have an extra empty line at the very top of the output.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Move line 11 to line 20.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  I have an extra empty line at the very top of the output.

Where in the code is there a statement that prints an empty line?
Is the statement in the correct location?
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 11.
Not in the correct location.
 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Line 11.
Not in the correct location.



This just makes an extra line at the bottom now.  Is it still technically correct?




 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacob Sousie wrote:This just makes an extra line at the bottom now.  Is it still technically correct?

This does not "create an extra line at the bottom". What this does is add a new-line character to the end of each string of #'s, including the last one.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think cleaning up your indentation would help with your understanding.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!