Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Nested loops  RSS feed

 
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I am in a beginners java class and I just cant figure out how to do these two nested loops can anyone help

1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8
3 4 5 6 7
4 5 6
5

and

    1
  212
32123
4321234
32123
   212
     1

thank so so much!
 
Marshal
Posts: 59092
180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

We don't hand out complete answers, but if you show us what you have got so far, we'll be happy to comment on it.
 
Ryan Arroyo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay I am on my phone right now I will show what I have so far
 
Campbell Ritchie
Marshal
Posts: 59092
180
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So hw far have you got with it? Writing that sort of loop is often awkward; it is better to start with something simpler and buld up the application.
Try printing

1

then

1 2

then

1 2 3

etc. That will be a start.
 
Sheriff
Posts: 5691
393
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

requirement wrote:1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8
3 4 5 6 7
4 5 6
5


Also write down on a piece of paper what pattern you notice here. What rules are being applied. That should be simple to work out with a pencil on a piece of paper.
 
Ryan Arroyo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

this is what i have so far for the pyramid the other one It keeps printing out only 9

Moderator edit: added code tags
 
Ryan Arroyo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and this is what i have for the other one

 
Ryan Arroyo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
UPDATED


prints

Method 15:
123456789
12345678
1234567
123456
12345
End Method 15
 
Sheriff
Posts: 4865
136
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks a lot like you're guessing.  Not the best way to write a program.

Hint: what is the relationship between the rows and columns?
 
please buy this thing and then I get a fat cut of the action:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!