java龟兔赛跑代码 java如何用线程实现龟兔赛跑
java写一个龟兔赛跑
public static void main(String[] args) {
成都创新互联-专业网站定制、快速模板网站建设、高性价比环翠网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式环翠网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖环翠地区。费用合理售后完善,10余年实体公司更值得信赖。
// TODO Auto-generated method stub
int a = 0, b = 0;
int second = 0;
do {
int number = new Random().nextInt(10) + 1;// 产生1-10随机数
// 如果随机数小于等于8,则表示80%的概率
if (number = 8) {
a = a + 2;
}
// 如果随机数小于等于5,则表示50%的概率
if (number = 5) {
b = b + 4;
}
second++;
System.out.println("第" + second + "秒:乌龟跑了" + a + ",兔子跑了" + b);
} while (a 100 b 100);
}
不懂地方可以追问。
求java龟兔赛跑程序代码!
import java.util.*;
public class Test{
public static void main(String[] args)throws Exception {
float t,g,m=0,num=0;
float tt,gg;
int q=0;
Scanner s;
System.out.println("输入兔子跑一圈时间/秒:");
s=new Scanner(System.in);
t=s.nextFloat();
System.out.println("输入乌龟跑一圈时间/秒:");
s=new Scanner(System.in);
g=s.nextFloat();
tt=(float)1/(t*1000);
gg=(float)1/(g*1000);
System.out.println("赛跑开始……");
while(true){
try{
Thread.sleep(10);
}catch(Exception e){}
m+=10;
if((int)((tt-gg)*m)q){
q=(int)((tt-gg)*m);
System.out.println("在第"+m/1000+"秒");
System.out.println("兔子超过乌龟"+q+"圈");
}
}
}}
求龟兔赛跑Java代码
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龟兔赛跑代码 java如何用线程实现龟兔赛跑
地址分享:http://pwwzsj.com/article/ddcpgep.html