求JAVA高手们帮我看下我的程序有什么问题。
publicclassTest8_1{/***@paramargs*/publicstaticvoidmain(String[]args){TicketHouseoffi...
public class Test8_1 {
/**
* @param args
*/
public static void main(String[] args) {
TicketHouse officer=new TicketHouse();
Thread wanger,zhangsan,lisi;
wanger=new Thread(officer);
wanger.setName("王二");
zhangsan=new Thread(officer);
zhangsan.setName("张三");
lisi=new Thread(officer);
lisi.setName("李四");
wanger.start();
zhangsan.start();
lisi.start();
// TODO Auto-generated method stub
}
}
public class TicketHouse implements Runnable{
int fiveAmount=1,tenAmount=0,twentyAmount=0;
public void run(){
if(Thread.currentThread().getName().equals("王二")){
saleTicket(20);
}
else if(Thread.currentThread().getName().equals("张三")){
saleTicket(5);
}
else if(Thread.currentThread().getName().equals("李四")){
saleTicket(5);
}
}
private synchronized void saleTicket(int money){
if(money==5){
fiveAmount=fiveAmount+1;
System.out.println("给"+Thread.currentThread().getName()+"入场券,"+Thread.currentThread().getName()+"的钱正好");
}
else if(money==20){
while(fiveAmount<3){
try{System.out.println("\n"+Thread.currentThread().getName()+"靠边站...");
wait();
System.out.println("\n"+Thread.currentThread().getName()+"继续买票");
}
catch(InterruptedException e){}
}
fiveAmount=fiveAmount-3;
twentyAmount=twentyAmount+1;
System.out.println("给"+Thread.currentThread().getName()+"入场券,"+Thread.currentThread().getName()+"给20,找赎15元");
}
notifyAll();
}
}
eclipse系统显示public class TicketHouse implements Runnable{这一行中错误:
The public type TicketHouse must be defined in its own file
请问具体该怎么修改。。是不是TicketHouse类 的位置放错了。。 展开
/**
* @param args
*/
public static void main(String[] args) {
TicketHouse officer=new TicketHouse();
Thread wanger,zhangsan,lisi;
wanger=new Thread(officer);
wanger.setName("王二");
zhangsan=new Thread(officer);
zhangsan.setName("张三");
lisi=new Thread(officer);
lisi.setName("李四");
wanger.start();
zhangsan.start();
lisi.start();
// TODO Auto-generated method stub
}
}
public class TicketHouse implements Runnable{
int fiveAmount=1,tenAmount=0,twentyAmount=0;
public void run(){
if(Thread.currentThread().getName().equals("王二")){
saleTicket(20);
}
else if(Thread.currentThread().getName().equals("张三")){
saleTicket(5);
}
else if(Thread.currentThread().getName().equals("李四")){
saleTicket(5);
}
}
private synchronized void saleTicket(int money){
if(money==5){
fiveAmount=fiveAmount+1;
System.out.println("给"+Thread.currentThread().getName()+"入场券,"+Thread.currentThread().getName()+"的钱正好");
}
else if(money==20){
while(fiveAmount<3){
try{System.out.println("\n"+Thread.currentThread().getName()+"靠边站...");
wait();
System.out.println("\n"+Thread.currentThread().getName()+"继续买票");
}
catch(InterruptedException e){}
}
fiveAmount=fiveAmount-3;
twentyAmount=twentyAmount+1;
System.out.println("给"+Thread.currentThread().getName()+"入场券,"+Thread.currentThread().getName()+"给20,找赎15元");
}
notifyAll();
}
}
eclipse系统显示public class TicketHouse implements Runnable{这一行中错误:
The public type TicketHouse must be defined in its own file
请问具体该怎么修改。。是不是TicketHouse类 的位置放错了。。 展开
3个回答
展开全部
eclipse要求每个类都要有单独的文件,你只要再建一个TicketHouse的类文件就好了
代码什么的都不用改
代码什么的都不用改
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你是不是把两个类写在同一个文件中了
追问
额 是啊 。。要怎么样改 啊
追答
分别建两个类Test8-1和TicketHouse,代码不变再运行
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询