龟兔赛跑相关问题
兔子类:public class Rabbit extends Thread
{
String threadName;
public Rabbit(String threadName){
this.threadName=threadName;
}
public void run(){
int length=0;
while(length<100){
try{
Thread.sleep(1000);//stop 1 sec everytime
}
catch(InterruptedException e){
e.printStackTrace();
}
length+=10;
System.out.println(this.threadName+"has run :"+length+"miles");
}
System.out.println(Thread.currentThread().getName()+"reached!!");
}
}
乌龟类:
public class Tortoise implements Runnable
{ String threadName;
public Tortoise(String threadName){
this.threadName=threadName;
}
public void run()
{
for(int length=0;length<100;length+=10){
System.out.println(this.threadName+"has run :"+length+"miles");
}
System.out.println(Thread.currentThread().getName()+"reached!!");
}
}
主方法所在的乌龟兔子类:
public class RabbitTortoise
{
public static void main(String[] args)
{ Tortoise tortoise = new Tortoise(tortoise);
Rabbit rabbit = new Rabbit(rabbit);
tortoise.setPriority(3);//优先级3
rabbit.setPriority(1);//优先级1
Thread thread1 = new Thread(tortoise);
Thread thread2 = new Thread(rabbit);
thread1.start();
thread2.start();
}
}
运行结果RabbitTortoise 三处出错;
C:\Documents and Settings\KevinGarnett\桌面\Java\7\RabbitTortoise.java:4: 找不到符号
符号: 构造函数 Tortoise(Tortoise)
位置: 类 Tortoise
{ Tortoise tortoise = new Tortoise(tortoise);
^
C:\Documents and Settings\KevinGarnett\桌面\Java\7\RabbitTortoise.java:5: 找不到符号
符号: 构造函数 Rabbit(Rabbit)
位置: 类 Rabbit
Rabbit rabbit = new Rabbit(rabbit);
^
C:\Documents and Settings\KevinGarnett\桌面\Java\7\RabbitTortoise.java:6: 找不到符号
符号: 方?setPriority(int)
位置: 类 Tortoise
tortoise.setPriority(3);//优先级3
应该如何改动?