Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes Stars program help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Stars program help" Watch "Stars program help" New topic

Stars program help

Bryce Heath

Joined: Sep 16, 2011
Posts: 19
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.

Tim Chaung

Joined: Sep 19, 2011
Posts: 17
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

Joined: Sep 16, 2011
Posts: 19
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.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11153

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Bryce Heath

Joined: Sep 16, 2011
Posts: 19
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?
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37941
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

Joined: Oct 13, 2005
Posts: 37941
There was only one misspelling, which I have corrected.
I agree. Here's the link:
subject: Stars program help
Similar Threads
paintComponent(page), Reverse digit, and Asterisks triangle outputter question
How to read from all files in a directory & plot the contents by processing contents?
for loops