【#文档大全网# 导语】以下是®文档大全网的小编为您整理的《龟兔赛跑文字版,亲自试验过》,欢迎阅读!
/*龟兔赛跑
总长100米,乌龟的时速:8m/s; 兔子的时速:10m/s 在兔子炮儿5秒后休息5S*/
package 实验十;
class a implements Runnable {
int count1=0 ;
private String st;
public a(String st){
this.st=st;}
synchronized public void run() {
if(st=="乌龟")
{
while(true)
{
try {
count1+=8;
System.out.println(st+count1+"米");
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();}
if(count1>100)
{
System.out.println("乌龟赢了"); System.exit(-1);
}
}
}
if(st=="兔子")
{
while(true)
{
try {
count1+=10;
System.out.println(st+count1+"米");
Thread.sleep(1000);
if(count1==50)
{System.out.println("兔子休息中");
Thread.sleep(3000);}
}
catch (InterruptedException e) {
e.printStackTrace();}
if(count1>100)
{
System.out.println("兔子赢了"); System.exit(-1);
}
}
}
}
}
public class rabbiter{
public static void main(String args[]){
new Thread(new a("乌龟")).start();
new Thread(new a("兔子")).start();
}
}