Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Nested loops  RSS feed

 
Ryan Arroyo
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!
 
Campbell Ritchie
Marshal
Posts: 56197
171
  • 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: 56197
171
  • 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.
 
Liutauras Vilda
Marshal
Posts: 4784
330
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
 
Knute Snortum
Sheriff
Posts: 4193
122
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?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!