aspose file tools*
The moose likes Beginning Java and the fly likes loop help? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "loop help?" Watch "loop help?" New topic
Author

loop help?

Emily Vieille
Greenhorn

Joined: Nov 27, 2006
Posts: 5
I'm a complete beginner to JAVA so all of this is kind of confusing to me. but in my computer science class we have to figure out a couple of things with loops.

for example, we have to print this out using nested loops:
2-----
-4----
--6---
---8--

any ideas? thanks <3
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11402
    
  16

Well, the more important question is do YOU have any ideas? Many folks who hang out here could write something to do this pretty quick, but that wouldn't help you learn anything. And that's what this site is about - helping you learn.

So, where are you in the process? Have you downloaded and installed the JRE? Do you know how to write and compile anything? Have you made an attempt at this problem, and if so, what worked or didn't work?

are you supposed to print it out EXACTLY like this, dashes and all, or did you just use those dashes to show us the spacing?

please post any code you have, and tell us exactly where you are stuck - does it compile? does it run at all? does it get stuck in a loop somewhere?

note that when you do post code, please use the "code" tags. you can click the button down below to get them, the just paste your code between the two tags - this will preserve your indentation, which makes the code MUCH easier for us to read.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Perhaps it may help to consider a simpler problem, then modify the solution. Could you write something to print this?

Or if that's too complex, how about this?


"I'm not back." - Bill Harding, Twister
Emily Vieille
Greenhorn

Joined: Nov 27, 2006
Posts: 5
ok thanks. well so far I have this

but I'm not sure how to do the dashes :/
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
All right, good. Now how about something like this:

Here I have replaced 1 dash with the number 1, 2 dashes with the number 2, etc. The idea is to hopefully find a mathematical relationship between the numbers on each line, without (yet) worrying about how to print, say, 3 dashes.

Again, if that seems like overload, you can first try a simpler version of the problem, ignoring the last column:
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
That +=2 was pretty good! We see the i++ so often it's easy to miss using other numbers to increment. The condition <10 might be confusing. Would you find it more like natural English to say <=8 ?

Now try the leading dashes. You can increment by 1 this time and build a string of dashes with String concatenation ...

dashes += "-";

In that loop i will equal 1,2,3,4. How could you quickly get 2,4,6,8 out of that?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
OK, Stan and I posted at about the same time, but chose different directions to go in. Really though, you can pick either one - whichever you have a better idea about. In either case, you want to modify your solution to something which is somehow closer to the final form. The order that you make these changes in is up to you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: loop help?