import java.util.*;
創(chuàng)新互聯(lián)建站是專業(yè)的鑲黃網(wǎng)站建設公司,鑲黃接單;提供成都網(wǎng)站設計、做網(wǎng)站,網(wǎng)頁設計,網(wǎng)站設計,建網(wǎng)站,PHP網(wǎng)站建設等專業(yè)做網(wǎng)站服務;采用PHP框架,可快速的進行鑲黃網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
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+"圈");
}
}
}}
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毫秒。誰先到終點就結(jié)束程序
* ,并顯示獲勝方。)
* @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("比賽結(jié)束:"+new Date(temp_lastTime)+"毫秒");
System.out.println("所耗時間:"+(temp_lastTime-temp_actionTime)+"毫秒");
System.out.println("兔子="+t.rabbit_walk+" 烏龜="+t.tortoise_walk);
}
}
//不知道兔子和烏龜?shù)牟介L時間是否按每秒。 這里程序只考慮依次遞增頻率
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();
}
}