龟兔赛跑: --普通格,共100格 龟:每次随机移动1至3格 兔子:每次随机移动1至6格 每20格会随机出现以下功能格各一次(不会在同一格内出现两种或两种以上功能) 功能格:(**和^^可以连续执行,@@和==不能连续执行) **幸运星格:选手可再行动一次 @@地雷格:兔子回到上数两个地雷格,龟回到上一地雷格(若后方无地雷格则回到起点) ==传送格:兔子到达下一传送格,龟到达下数两个传送格(若前方无传送格则到达终点) ||树格:兔子停止三次行动^^下坡格:龟前进10格
import java.applet; import java.awt; class RunningAnimal extends Thread { int distance; int speed; int sleeptime; int length; List list; String name; public void run() { while (distance < length){ distance+=speed; try { sleep(sleeptime); } catch (InterruptedException e) { } list.addItem(name+":"+distance); } list.addItem(name+" arrives."); } RunningAnimal(String aname,int alldistance ,int aspeed,int asleeptime,List alist) { name=aname; length=alldistance; speed=aspeed; sleeptime=asleeptime; distance=0; list=alist; } } public class TestThread extends Applet { RunningAnimal turtle; RunningAnimal rabit; public void init() { setLayout(new BorderLayout()); List lList=new List(); add("Center",lList); turtle=new RunningAnimal("Turtle",500,20,20,lList); turtle.setPriority(7); turtle.start(); rabit=new RunningAnimal("Rabit",500,300,500,lList); rabit.setPriority(3); rabit.start(); } }
龟兔赛跑Java代码
最新推荐文章于 2022-05-13 10:08:14 发布