Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 5 of 5 FirstFirst ... 345
Results 101 to 106 of 106

Thread: Car racing

  1. #101
    Member
    Join Date
    Jul 2011
    Posts
    54
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Car racing

    Quote Originally Posted by Norm View Post
    Yes, that is how I would do it.


    No, do the increment outside of the println in its own statement.

    A sample of printing:
    int laps = 3; // for testing only
    System.out.println("Car laps=" + laps); // print the value of laps
    laps++;
                System.out.println("Racer 1 has completed " + laps + " lap(s)");
    Is this correct?Or is it like that?
    for(int laps = 0; laps < 4; laps++){
                    System.out.println("Racer 1 has completed " + laps + " lap(s)");}
    Last edited by DanielVkc; July 22nd, 2011 at 12:05 PM.

  2. #102
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Car racing

    It looks like it could work. What happens when you compile and execute the program?

  3. #103
    Member
    Join Date
    Jul 2011
    Posts
    54
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Car racing

    Quote Originally Posted by Norm View Post
    It looks like it could work. What happens when you compile and execute the program?
    It will print out :
    Racer 1 has completed 0 lap(s)
    Racer 1 has completed 1 lap(s)
    Racer 1 has completed 2 lap(s)
    Racer 1 has completed 3 lap(s)
    It displays all of these messages in one time.
    Last edited by DanielVkc; July 22nd, 2011 at 12:14 PM.

  4. #104
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Car racing

    Where are the messages for the other two cars?

    When does your code print out the "completed" message?

  5. #105
    Member
    Join Date
    Jul 2011
    Posts
    54
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Car racing

    Quote Originally Posted by Norm View Post
    Where are the messages for the other two cars?

    When does your code print out the "completed" message?
    Racer 1 has completed 0 lap(s)
    Racer 1 has completed 1 lap(s)
    Racer 1 has completed 2 lap(s)
    Racer 1 has completed 3 lap(s)
    Racer 2 has completed 0 lap(s)
    Racer 2 has completed 1 lap(s)
    Racer 2 has completed 2 lap(s)
    Racer 2 has completed 3 lap(s)
    Racer 3 has completed 0 lap(s)
    Racer 3 has completed 1 lap(s)
    Racer 3 has completed 2 lap(s)
    Racer 3 has completed 3 lap(s)
    This is the complete messages for all 3 cars.But it displays everything at one time.
    Once they started the race their messages are printed.

  6. #106
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Car racing

    Once they started the race their messages are printed.
    That does not make any sense.
    Change the code to print out the message when a car has completed a lap.

    BTW How much of the code you posted for this program did you write? You don't seem to understand how it works.

Page 5 of 5 FirstFirst ... 345