fictive
发贴: 3
积分: 0
|
于 2005-11-08 19:33
我按照书的例子编了一个关于售票的小程序。可编译通过,但不能执行。各位高手能帮我看看错在哪吗? import java.applet.*; import java.awt.*; import java.awt.event.*; public class Example_10 extends Applet implements Runnable { Thread five,ten,twenty; int 五圆=2,十圆=0,二十圆=0; static TextArea text; String s=null; public void init() { five=new Thread(this); ten=new Thread(this); twenty=new Thread(this); text=new TextArea(15,20); add(text); } public void start() { twenty.start(); ten.start(); five.start(); } public void run() { if(Thread.currentThread()==five) { 售票规则(5); } else { if(Thread.currentThread()==ten) { 售票规则(10); } else { if(Thread.currentThread()==twenty) { 售票规则(20); } } } } public synchronized void 售票规则(int money) { switch (money) { case 5: 五圆+=1; s="您的钱正好,给你票"; Example_9.text.append(s+"\n"); break; case 10: while (五圆<1) { try { wait(); } catch(InterruptedException e){} } 五圆-=1; 十圆+=1; s="找你五圆,给你票"; Example_9.text.append(s+"\n"); break; case 20: while(!((五圆>0)&&(十圆>0))&&(五圆<3)) { try { wait(); } catch(InterruptedException e){} } if(五圆<3) { 十圆-=1; 五圆-=1; } else { if (十圆<1) { 五圆-=3; } } s="找你十五圆钱,你的票"; Example_9.text.append(s+"\n"); break; } notifyAll(); } }
|