Java Implementation Turtle and rabbit race

Source: Internet
Author: User

Idle extremely bored, plus turn over the mobile phone to see the Turtle and rabbit race words, thought can through Java two threads to achieve the turtle and rabbit race.

The code implementation is actually simple:

The first is the Turtle class:

And then the Rabbit class:

Finally the race class:

Then let's look at the output:

The turtle is starting to play.
The rabbit is starting to play.
The rabbit ran 0 meters.
The turtle ran 0 meters.
The turtle ran 3 meters.
The rabbit ran 8 meters.
The turtle ran 4 meters.
The rabbit ran 16 meters.
The turtle ran 5 meters.
The rabbit ran 21 meters.
The turtle ran 5 meters.
The rabbit ran 28 meters.
The rabbit ran 34 meters.
The turtle ran 10 meters.
The turtle ran 13 meters.
The rabbit ran 39 meters.
The turtle ran 19 meters.
The rabbit ran 48 meters.
The rabbit ran 53 meters.
The turtle ran 22 meters.
The rabbit ran 61 meters.
The turtle ran 23 meters.
The turtle ran 29 meters.
The rabbit ran 66 meters.
The rabbit ran 75 meters.
The turtle ran 35 meters.
The rabbit ran 81 meters.
The turtle ran 37 meters.
The turtle ran 39 meters.
The rabbit ran 85 meters.
The turtle ran 41 meters.
The rabbit starts to sleep
The turtle ran 44 meters.
The turtle ran 46 meters.
The turtle ran 51 meters.
The turtle ran 55 meters.
The turtle ran 55 meters.
The turtle ran 55 meters.
The turtle ran 56 meters.
The turtle ran 57 meters.
The turtle ran 58 meters.
The turtle ran 64 meters.
The turtle ran 69 meters.
The turtle ran 74 meters.
The turtle ran 74 meters.
The turtle ran 80 meters.
The turtle ran 84 meters.
The turtle ran 84 meters.
The turtle ran 84 meters.
The turtle ran 86 meters.
The turtle ran 88 meters.
The turtle ran 91 meters.
The turtle ran 92 meters.
The turtle ran 98 meters.
The turtle reached the end.
The rabbit ran 92 meters.
The rabbit ran 96 meters.
The rabbit reached the end.

is not particularly complex code, but simple implementation of the process by inheriting the thread to carry out a simple process management, I hope to help you, the code is still a picture, the hand knocks good health.

Java Implementation Turtle and rabbit race

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.