Results 1 to 11 of 11

Thread: While loop

  1. #1
    Join Date
    Dec 2011
    Location
    Germany
    Posts
    9
    Rep Power
    0

    Default 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?

    lucas
    Last edited by java_beginner84; 12-28-2011 at 06:32 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,702
    Rep Power
    8

    Default 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!

  3. #3
    Join Date
    Dec 2011
    Location
    Germany
    Posts
    9
    Rep Power
    0

    Default 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)

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,702
    Rep Power
    8

    Default 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!

  5. #5
    Join Date
    Dec 2011
    Location
    Germany
    Posts
    9
    Rep Power
    0

    Default 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

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,702
    Rep Power
    8

    Default 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!

  7. #7
    Join Date
    Dec 2011
    Location
    Germany
    Posts
    9
    Rep Power
    0

    Default Re: While loop

    ok i will try to create an SSCCE then and post it asap, thank you anyway

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,702
    Rep Power
    8

    Default Re: While loop

    Quote Originally Posted by java_beginner84 View Post
    ok i will try to create an SSCCE then and post it asap, thank you anyway
    The main question is- are you having trouble causing the while loop to stop between iterations of the loop, or are you trying to exit the while loop in the middle of an iteration? An sscce will help clear that up.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Join Date
    Dec 2011
    Location
    Germany
    Posts
    9
    Rep Power
    0

    Default Re: While loop

    I guess the second one, trying to exit the while loop in the middle of an iteration, is it possible?

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,702
    Rep Power
    8

    Default 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!

  11. #11
    Join Date
    Dec 2011
    Location
    Germany
    Posts
    9
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  2. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  3. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  4. [Q] Loop issue (while loop)
    By iriscience in forum New To Java
    Replies: 9
    Last Post: 01-31-2011, 04:21 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •