Results 1 to 11 of 11
Thread: While loop
- 12-28-2011, 06:09 PM #1
Member
- Join Date
- Dec 2011
- Location
- Germany
- Posts
- 9
- Rep Power
- 0
While loop
Hello guys
I'm trying to write a simple class RaceOfJumpingCars , i have all methods done, but one of them doesn't work as it should.
Method advanceAllCars (no arguments, no return value)should move all cars refrenced by racingCar1, racingCar2 and racingCar3, they only can move from position 1 by randomly generated numbers from 1 and 3 inclusive.and they should move one by one But when the first car reach position 15 it wins the race and the other cars stop moving and the method should does nothing
my approach is
public void advanceAllCars()
{
while(this.raceOver !=true)
{
if(this.getRacingCar1().getPosition()<=15)
{
this.advanceRacingCar(racingCar1);//using another method advanceRacing Car
}
if(this.getRacingCar2().getPosition()<=15)
{
this.advanceRacingCar(racingCar2);
}
if(this.getRacingCar3().getPosition()<=15)
{
this.advanceRacingCar(racingCar3);
}
}
}
But for example when one of the cars reaches the 15 position , other still moving,
please guys give me an advice what to change in my loop?
lucasLast edited by java_beginner84; 12-28-2011 at 06:32 PM.
- 12-28-2011, 06:12 PM #2
Re: While loop
And when do you set that boolean to true?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 12-28-2011, 06:20 PM #3
Member
- Join Date
- Dec 2011
- Location
- Germany
- Posts
- 9
- Rep Power
- 0
Re: While loop
is set to boolean true when the race is over, so in the beginning i put that the race is still on (!=true)
- 12-28-2011, 06:24 PM #4
Re: While loop
So does the while loop continue executing or not? This would be much easier if you simply posted an SSCCE that demonstrated exactly what you're talking about, because at this point you could mean a few different things, and you haven't posted enough code for us to be sure about what's going on anyway.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 12-28-2011, 06:36 PM #5
Member
- Join Date
- Dec 2011
- Location
- Germany
- Posts
- 9
- Rep Power
- 0
Re: While loop
this is the only code i have for that method , i cant post it all as it is homework and could break the rule, i just need to know how to stop loop after first car will reach position 15. Basically after the method is executed, racingCar1,racingCar2 and racingCar3 moving one after one by 1,2 or 3 positions until one of them will reach position 15 first, then the other two need to stop moving. If you are able to see that the while loop i wrote is correct, i would probably need to implement better the rest of my methods
- 12-28-2011, 06:42 PM #6
Re: While loop
If this is a homework assignment, that's all the more reason to create an SSCCE that demonstrates the ideas behind what you're talking about instead of the actual details. I can't really help you until I see one, so it's up to you.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 12-28-2011, 06:44 PM #7
Member
- Join Date
- Dec 2011
- Location
- Germany
- Posts
- 9
- Rep Power
- 0
Re: While loop
ok i will try to create an SSCCE then and post it asap, thank you anyway
- 12-28-2011, 06:46 PM #8
Re: While loop
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 12-28-2011, 06:52 PM #9
Member
- Join Date
- Dec 2011
- Location
- Germany
- Posts
- 9
- Rep Power
- 0
Re: While loop
I guess the second one, trying to exit the while loop in the middle of an iteration, is it possible?
- 12-28-2011, 06:55 PM #10
Re: While loop
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 12-28-2011, 07:07 PM #11
Member
- Join Date
- Dec 2011
- Location
- Germany
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Problem with while loop, assigning a variable with a different value every loop? Help
By JavaProg in forum New To JavaReplies: 2Last Post: 11-07-2011, 02:25 AM -
Is it Possible? Array elements Initialized in Loop, can it be viewed outside loop?
By JPH in forum New To JavaReplies: 1Last Post: 10-01-2011, 02:12 AM -
JTextField loop 2x for-loop WEIRD!
By Streetproject in forum AWT / SwingReplies: 2Last Post: 02-16-2011, 05:46 PM -
[Q] Loop issue (while loop)
By iriscience in forum New To JavaReplies: 9Last Post: 01-31-2011, 04:21 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks