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
• Ron McLeod
• Tim Cooke
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Paul Clapham
• Rob Spoor
• Junilu Lacar
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Piet Souris
• Carey Brown
Bartenders:

# Stars program help

Greenhorn
Posts: 19
• Number of slices to send:
Optional 'thank-you' note:
Hey guys
Working on my for loops and trying to make a diamond shape with this program, but for some reason my second for is making 3 stars when it should only do 2. Ill keep working on it but any ideas would be great.

Greenhorn
Posts: 17
• Number of slices to send:
Optional 'thank-you' note:
I'm guessing at what you're trying to say....
and I think your problem is that in your second series of for loops.. you have
for (int row = 2; row > MIN_ROWS; row--)

it will make 3 stars because it takes 3 steps to get to below zero. changing it to a '>=' should fix it?

Bryce Heath
Greenhorn
Posts: 19
• Number of slices to send:
Optional 'thank-you' note:
Hey thanks for the quick reply, I tried that also with no luck. Im not really sure whats up with it because with MIN_ROW being 0 if I did for (int row = 2; row > MIN_ROW; row--) wouldent that set row to 2 run the loop decrease row to 1 run the loop a second time decrease row to 0 making the row> MIN_ROW end the loop because the 0 of row is now not greater but = to MIN_ROW? Hope that makes sense look forward to more posts on the subject.

lowercase baba
Posts: 13086
67
• Number of slices to send:
Optional 'thank-you' note:
put in print statements to see what is really going on. I changed your second nested loops to this (and am completely ignoring the first set of loops here):

and you get this:

C:\slop>java Stars
row: 2, star: 2
*row: 1, star: 2
*row: 1, star: 1
*

you come in, and both row and star are set to 2. you print the first star.

star gets decremented to 1. we test star >= row, which is false, so we exit the inner loop.

row is decremented to 1. 1 is > MIN_ROWS, so we enter the inner loop
star is set to 2.

star > row is true (2 >= 1), so we print again. we decrement star, making it 1.

we go back to the top of the inner loop, and check if star >= row (1 >=1), which is true, and we print another star (the 3rd one).

star is decremented to 0. 0 is NOT >= 1, so we exit the inner loop.

we decrement row, making it 0. 0 is NOT > MIN_ROWS, so we exit the outer loop.

Bryce Heath
Greenhorn
Posts: 19
• Number of slices to send:
Optional 'thank-you' note:
Hey guys thanks for the reply.
I guess my problem was that I dident understand nested for's right. I thought it would run the outer for, enter inner for, run inner for once and drop back out to the outer for to recheck again if the outer was still true then back into the inner. Want to make sure I am understanding it correctly when thinking that it will run the outer for go to inner for and keep running inner for till it is not longer true, then drop back to the outer for?

Marshal
Posts: 77168
370
• Number of slices to send:
Optional 'thank-you' note:
I think you have got it right, but try something like this, and you can see for yourself:I haven't run it yet, so it's probably full of misspellings which the compiler will complain about.

Campbell Ritchie
Marshal
Posts: 77168
370
• Number of slices to send:
Optional 'thank-you' note:
There was only one misspelling, which I have corrected.