• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Nested Loops

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can not figure out what is the prooooblem... Please help...

I need this pattern

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6

and here is my code...

import javax.swing.JOptionPane;

public class forLoop {
public static void main(String[] args) {
int row;
int column;

String outputPattern = "Pattern I\n";

for (row = 1; row <= 6; row++) {
for (column = 1; column <= row; column++) {
outputPattern += column + " ";
}
outputPattern += "\n";

System.out.println(outputPattern);
}
}
}

Instead of looking like the above pattern, it looks like:

Pattern I

1

Pattern I

1
1 2

Pattern 1

1
1 2
1 2 3

etc... etc... etc...

Please show me what a little mistake I am making...

Thanks in advance
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I could give you a hint, but you are already so close...

Basically, your print statement is in the wrong place. It is being called many times. Move your print statement outside of the outer loop.

Henry
 
Elise Wilson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OH MY GOODNESS!!! I knew it was something that was sooooo close...

THANK YOU SO MUCH!!! WOW @ your kindness... Other forums have some very *not so nice* people...



 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Elise Wilson:
... Other forums have some very *not so nice* people...


Yeah, I think we can guess where those "other forums" might be.
 
Elise Wilson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stumped again...

for (row = 1; row <= 6; row++) {
for (column = 6; column >= row; column--) {
outputPattern += column + " ";
}
outputPattern += "\n";
}

Looks like:
6 5 4 3 2 1
6 5 4 3 2
6 5 4 3
6 5 4
6 5
6

"SHOULD" look like this:

1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1

Since it is the flip of the first one, i did just that... It's not working... Please explain... All this +, >, -- is confusing...

 
Elise Wilson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Should look like (what happened to what I put):

For S&G purposes there aren't suppose to be * but it wouldn't take the spaces...

**********1
********2*1
******3*2*1
****4*3*2*1
**5*4*3*2*1
6*5*4*3*2*1
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can keep your space formatting intact by using the CODE tags when posting (or editing). So this is what you want...

Right?

Take this one row at a time. Suppose row = 1, and column goes from 6 to 1. For column values 6 through 2, we want a space. But for column = 1, we want the number 1. The trick is expressing this (the pattern of 5 spaces followed by the number "1") in terms of row and column values.

Next, suppose row = 2, and column again goes from 6 to 1. For column values 6 through 3, we want a space. But for column values 2 and 1, we want the numbers 2 and 1 respectively.

Do you see a pattern?
[ October 13, 2005: Message edited by: marc weber ]
 
Elise Wilson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By George I think I got it... My teacher doesn't "break it down" he just says to do it...

Thanks for all you guys help...

 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Elise Wilson:
... My teacher doesn't "break it down" he just says to do it...


Learning to approach problems in this manner is one of the more valuable skills you can develop as a programmer.
 
reply
    Bookmark Topic Watch Topic
  • New Topic