Forums Register Login

Small looping question

+Pie Number of slices to send: Send
I'm trying...honestly I am. But maybe some people just aren't cut out to learn programming. I'm really starting to wonder.

Anywho... I'm trying to write a small program that will print out a wedge of stars. Seems easy enough.

This is what I have so far:



Output should be:
Initial number of stars:
7
*******
******
*****
****
***
**
*


All I can figure out how to do is print the first line.

I know I'm missing something easy, I just can't wrap my head around it.
+Pie Number of slices to send: Send
 

All I can figure out how to do is print the first line.



Well, your loop only does one line. Can you use a loop to go through all the lines? Basically, have a loop within a loop?

Henry
+Pie Number of slices to send: Send
You have code that will do something once, and you want it to do that thing multiple times. That's exactly what loops are for.

So if you put your code that prints a line inside a loop, then you can print multiple lines.
+Pie Number of slices to send: Send
Another way to look at it is to encapsulate what you already know how to do in a method so that you can call it with different parameters. A call to the method might look like:

print(7, "*")

and the method signature would look like:

public void print(int times, String element)

once you've got that working (hint hint, you have the code for that one pretty much written) you can work out a loop that calls the method with different arguments:


until you're done. To my little brain that's easier to understand than nested loops, as always though, your mileage may vary.
+Pie Number of slices to send: Send
Thanks all for the help so far.

The lessons I'm following haven't touched on the 'for' loops yet, so I'm kinda stuck with 'if' and 'while' I think.

It's the nesting that's causing me such a headache. I'll keep punching it out though.
+Pie Number of slices to send: Send
 

Originally posted by Tod Novak:
...I'm kinda stuck with 'if' and 'while' I think.

It's the nesting that's causing me such a headache...


You can do this using nested while loops. The nesting itself is straight forward...

But you need to put some thought into what the while conditions are, and the other code that goes inside the loop bodies.
+Pie Number of slices to send: Send
'for' and 'while' loops are really interchangeable. Generally, if you know the exact number of loops, you'd use a 'for', otherwise a 'while', but that's really not written in stone.

here's how they would translate:


vs
+Pie Number of slices to send: Send
Woot!

Finally got it!



Not sure if it's as pretty as it could be, but it works.

Thank you all again for the guidance.
a wee bit from the empire
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1223 times.
Similar Threads
how to make this as an output ?
factors problem
Understanding logic on for loop
How to write an algorithm for an average of values?
How to "throw an exception?"
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:26:06.