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()
}
}
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+"圈")
}
}
}}
public class Test1{public static void main(String[] args) throws InterruptedException {
int v1=25,v2=10,t=20,s=3,l=100
Scanner scanner=new Scanner(System.in)
v1=scanner.nextInt()
v2=scanner.nextInt()
t=scanner.nextInt()
s=scanner.nextInt()
l=scanner.nextInt()
int sum1=0,sum2=0
boolean stop=false
int stopcount=0
int i=0
for ( i = 0 i < l/v2 i++) {
if(sum1>=l||sum2>=l)//如果有一个跑到了终点就结束了
樱拿 break
if(stopcount==s)
山亏 stop=false //如果休脊唯搭息的时间够了,就开始跑
if(sum1-sum2>=t)
stop=true //如果超过了t米,就休息
if(!stop)
sum1+=v1 //当兔子不休息的时候跑
else
stopcount++ //休息的时间计数
sum2+=v2//乌龟每次都会跑
System.out.print("兔子跑了:"+sum1+"米")
System.out.println("乌龟跑了:"+sum2+"米")
}
if(sum1==sum2)
System.out.println("D"+i)
else if(sum1>=l)
System.out.println("R"+i)
else if(sum2>=l)
System.out.println("T"+i)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)