class studentanswer extends Thread
{
private String name;
private String age;
private String school;
private String grade;
private String year;
private String month;
private String days;
private int x;
public void set(String name1, String age1, String school1, String grade1, String year1, String month1, String days1)
{
this.name = name1;
this.age = age1;
this.school = school1;
this.grade = grade1;
this.year = year1;
this.month = month1;
this.days = days1;
}
public void setint(int x1)
{
this.x = x1;
}
public String getname()
{
return name;
}
public String getage()
{
return age;
}
public String getschool()
{
return school;
}
public String getgrade()
{
return grade;
}
public String getyear()
{
return year;

}
public String getmonth()
{
return month;
}
public String getdays()
{
return days;
}
public int getint()
{
return x;
}
private void said()
{
switch(x)
{
case 0:
System.out.println(name+"说:我的名字叫"+name+".");
break;
case 1:
System.out.println(name+"说:我在"+school+"读书.");
break;
case 2:
System.out.println(name+"说:我现在在读"+grade+".");
break;
case 3:
System.out.println(name+"说:我今年"+age+"岁.");
break;
case 4:
System.out.println(name+"说:我学习软件开发"+year+"年.");
break;
case 5:
System.out.println(name+"说:今年"+month+"月放假.");
break;
case 6:
System.out.println(name+"说:一般放假的天数是"+days+"天.");
break;
case 7:
System.out.println(name+"说:不客气");
break;

}
}
public void run()
{
said();
try
{
sleep(1000);

}catch(Exception e){}
}
}
class teacherask extends Thread
{
private String name;
private int x;
public void set(String name)
{
this.name = name;

}
public String getname()
{
return name;
}
public void setint(int x)
{
this.x= x;
}
public int getint()
{
return x;
}
private void said()
{
switch(x)
{
case 0:
System.out.println(name+"说:你叫什么名字");
break;
case 1:
System.out.println(name+"说:你在哪所学校读书");
break;
case 2:
System.out.println(name+"说:你读什么系哪个年级");
break;
case 3:
System.out.println(name+"说:你今年多大了");
break;
case 4:
System.out.println(name+"说:你学习计算机软件开发多少年了");
break;
case 5:
System.out.println(name+"说:你几月放假");
break;
case 6:
System.out.println(name+"说:放假大概几天");
break;
case 7:
System.out.println(name+"说:谢谢你回答我的问题");
break;

}
}
public void run()
{
said();
try
{
sleep(1000);
}
catch(Exception e){}
}
}
public class test { /**
 * @param args
 */
public static void main(String[] args) {
studentanswer s1 = new studentanswer();
studentanswer s2 = new studentanswer();
studentanswer s3 = new studentanswer();
studentanswer s4 = new studentanswer();
studentanswer s5 = new studentanswer();
studentanswer s6 = new studentanswer();
studentanswer s7 = new studentanswer();
studentanswer s8 = new studentanswer();
teacherask t1 = new teacherask();
teacherask t2 = new teacherask();
teacherask t3 = new teacherask();
teacherask t4 = new teacherask();
teacherask t5 = new teacherask();
teacherask t6 = new teacherask();
teacherask t7 = new teacherask();
teacherask t8 = new teacherask();
studentanswer[] stt = new studentanswer[]{s1,s2,s3,s4,s5,s6,s7,s8};
teacherask[] stt1 = new teacherask[]{t1,t2,t3,t4,t5,t6,t7,t8};
try
{
for(int x=0; x<8; x++)
{
stt[x].set("TOM", "22", "重庆大学", "自动化系二年      级", "3", "7", "54");
stt1[x].set("John老师");
stt1[x].setint(x);
stt[x].setint(x);
stt1[x].start();
stt[x].start();


}
}catch(Exception e){}[color=#FF0000]


// TODO Auto-generated method stub }}[/color]运行结果是:
John老师说:你叫什么名字
TOM说:我的名字叫TOM.
TOM说:我在重庆大学读书.
John老师说:你在哪所学校读书
John老师说:你读什么系哪个年级
TOM说:我现在在读自动化系二年级.
TOM说:我今年22岁.
John老师说:你今年多大了
John老师说:你学习计算机软件开发多少年了
TOM说:我学习软件开发3年.
John老师说:你几月放假
TOM说:今年7月放假.
John老师说:放假大概几天
TOM说:一般放假的天数是54天.
John老师说:谢谢你回答我的问题
TOM说:不客气
不知道哪里出问题了啊,跟所需求的结果不同啊,所需求的结果应该是:
John老师说:你叫什么名字
TOM说:我的名字叫TOM.
John老师说:你在哪所学校读书
TOM说:我在重庆大学读书.
John老师说:你读什么系哪个年级
TOM说:我现在在读自动化系二年级.
John老师说:你今年多大了
TOM说:我今年22岁.
John老师说:你学习计算机软件开发多少年了
TOM说:我学习软件开发3年.
John老师说:你几月放假
TOM说:今年7月放假.
John老师说:放假大概几天
TOM说:一般放假的天数是54天.
John老师说:谢谢你回答我的问题
TOM说:不客气

解决方案 »

  1.   

    按楼主的设计,只要让两个线程稍微延迟启动就可以. for(int x=0; x<8; x++)
    {
    stt[x].set("TOM", "22", "重庆大学", "自动化系二年      级", "3", "7", "54");
    stt1[x].set("John老师");
    stt[x].setint(x);
    stt1[x].setint(x);
    stt1[x].start();//老师提问先启动.之后延时0.5秒,学生线程启动.
    try
    {
    Thread.sleep(500);
    }
    catch(InterruptedException ie)
    {
    ie.printStackTrace();
    }
    stt[x].start();
    try //学生线程启动后,再延时一会儿.
    {
    Thread.sleep(500);
    }
    catch(InterruptedException ie)
    {
    ie.printStackTrace();
    }
    }