赛跑java代码 龟兔赛跑编程java
求java龟兔赛跑程序代码!
import java.util.*;
创新互联建站是专业的镶黄网站建设公司,镶黄接单;提供成都网站设计、做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行镶黄网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
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.util.Date;
public class Test extends Thread{ private int tortoise_walk = 0; // 乌龟已跑长度存放变量
private int rabbit_walk = 0; // 兔子已跑长度存放变量
private int finish = 1000; // 终点
private volatile boolean hasWinner = false;// 胜利者诞生 /**
*
* @ClassName: Tortoise_Run
* @Description: TODO(乌龟奔跑线程)
* @author guotingchao
* @date 2012-3-6 上午10:20:45
*
*/
class Tortoise_Run implements Runnable {
@Override
public void run() {
try {
while (!hasWinner) {
if (tortoise_walk % 100 == 0 (tortoise_walk != 0||tortoise_walk=finish)) { //乌龟每100米休息500毫秒
System.out.println("乌龟休息中………………");
Thread.sleep(500);
}
tortoise_walk++;
System.out.println("乌龟已跑"+tortoise_walk+"米");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} /**
*
* @ClassName: Rabbit_Run
* @Description: TODO(兔子奔跑线程)
* @date 2012-3-6 上午10:25:10
* @author guotingchao
*/
class Rabbit_Run implements Runnable {
@Override
public void run() {
try {
while (!hasWinner) {
if (rabbit_walk % 20 == 0 (rabbit_walk != 0||rabbit_walk=finish)) { //兔子每20米休息500毫秒
System.out.println("兔子休息中………………");
Thread.sleep(500);
}
rabbit_walk=rabbit_walk+5; //每秒跑5步
System.out.println("兔子已跑"+rabbit_walk+"米");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void run(){
new Thread(new Rabbit_Run()).start();
new Thread(new Tortoise_Run()).start();
}
/**
* @Title: main
* @Description: TODO(
* 赛程1000米,兔子跑5米,乌龟跑1米,兔子每20米休息500毫秒,乌龟每100米休息500毫秒。谁先到终点就结束程序
* ,并显示获胜方。)
* @param @param args
* @param @throws Exception 设定文件
* @author guotingchao
* @return void 返回类型
* @throws
*/
public static void main(String[] args) throws Exception {
long temp_actionTime=System.currentTimeMillis();
System.out.println("比赛开始:"+new Date(temp_actionTime)+"毫秒");
Test t=new Test();
new Thread(t).start();
while(true){
if(t.tortoise_walk=t.finish||t.rabbit_walk=t.finish){
t.hasWinner=true;
break;
}
}
String winnnerName=t.tortoise_walkt.rabbit_walk?"乌龟":"兔子";
long temp_lastTime=System.currentTimeMillis();
System.out.println(winnnerName+"胜利");
System.out.println("比赛结束:"+new Date(temp_lastTime)+"毫秒");
System.out.println("所耗时间:"+(temp_lastTime-temp_actionTime)+"毫秒");
System.out.println("兔子="+t.rabbit_walk+" 乌龟="+t.tortoise_walk);
}
}
//不知道兔子和乌龟的步长时间是否按每秒。 这里程序只考虑依次递增频率
求龟兔赛跑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/hhggee.html