public class ThreeThread public static void main(String args){ SpeakElephant speakElephant: SpeakCar speakCar; speakElephant =new SpeakElephantO; 【1】 speakCar=new SpeakCar); 【2】 for (int i=1;i<=15;i++) System.out.print("主人"+i+""): } class SpeakElephant_【3】{ public void runO{ for (int i=1;i<=20;i++ System.out print("大象"+i+""); } class SpeakCar【4】{ public void run){ for (int i=1;i<=20;i++) System.out.print("轿车"+i+"");
时间: 2024-01-13 14:03:31 浏览: 75
这段代码有很多语法错误,我做了一些修改,如下所示:
```
public class ThreeThread {
public static void main(String[] args) {
SpeakElephant speakElephant;
SpeakCar speakCar;
speakElephant = new SpeakElephant();
speakCar = new SpeakCar();
for (int i = 1; i <= 15; i++) {
System.out.print("主人" + i + ": ");
new Thread(speakElephant).start();
new Thread(speakCar).start();
}
}
}
class SpeakElephant implements Runnable {
public void run() {
for (int i = 1; i <= 20; i++) {
System.out.print("大象" + i + ",");
}
}
}
class SpeakCar implements Runnable {
public void run() {
for (int i = 1; i <= 20; i++) {
System.out.print("轿车" + i + ",");
}
}
}
```
这段代码的功能是创建三个线程,其中两个线程分别输出"大象1,大象2,...,大象20,"和"轿车1,轿车2,...,轿车20,",第三个线程则输出"主人i: "。在主函数中,创建15个主人,并让他们同时启动"大象"线程和"轿车"线程。
阅读全文