"For" loop help

Hey guys, I'm new to the Java scene, and currently doing an assignment. Part of the assignment we're required to do use a "for" loop to make our vehicle move forward.

The instruction is basically:

Put code into the "ticker" method that loops over each cars in the "cars" array, and sends a message to each car telling it to drive()

I'm in the "ticker" class and inputed:

for (car = 0, car > 1.5, car++)

{

}

I'm stuck as in what I should put in the loop to allow me to tell the car to drive.

So sorry if I left anything out.

Cheers

[606 byte] By [Rayman84a] at [2007-11-27 5:03:34]
# 1

car > 1.5

For starters, I hope you realise the above is false and therefore your for loop will never be entered.

You have an array that is filled with Car objects, is that correct? Then you need to iterate over that array and access each Car object one at a time. You can then call methods on that Car object, one of which is drive().

floundera at 2007-7-12 10:21:39 > top of Java-index,Java Essentials,New To Java...
# 2

> I'm in the "ticker" class and inputed:

>

> for (car = 0, car > 1.5, car++)

> {

>

> }

Oh... this is a double fault... :)

The "for" loop may contains commas (,), but the three looping component (init, condition, iteration) must be separated with semicolons (;). If the condition true, the loop will process, and this condition will evaluate again (and again, ...) while condition will be false. Your condition will be false in first run: "car > 1.5", which "car" value is 0.

for (car=0; car < 1.5; car++)

{

[...]

}

--

http://www.javaforum.hu

auth.gabora at 2007-7-12 10:21:39 > top of Java-index,Java Essentials,New To Java...