急需java程序设计代码

来源:百度知道 编辑:UC知道 时间:2024/09/23 06:32:27
用线程方法实现龟兔赛跑,龟和兔可以用两个点表示,看他们哪个点先到终点。

有点概念了,正在做……

写了个基本代码出来,你可以自己改改:

public class Game extends Thread {

int distance = 46;
boolean run = true;
Rabbit rabbit = new Rabbit(this);
Turtle turtle = new Turtle(this);

@Override
public void run() {
while(run) {
if(!rabbit.end)
rabbit.run();
if(!turtle.end)
turtle.run();
check();
}
}

public void check() {
if(rabbit.end && turtle.end) {
run = false;
}
}

public static void main(String[] args) {
Game game = new Game();
game.start();
}
}

abstract class Racer {

int space;
Game game;
boolean end;

public Racer(Game game) {
this.game = game;
}

public void run() {System.out.println("run:space=" + space);
if(!isRest()) {
space += getSpeed();
if(check()) {
end();