本帖最后由 武嘉豪 于 2013-9-6 21:15 编辑
- import java.util.Timer;
- class road extends java.util.TimerTask
- {
- private String name;
- private int cum=0;//cum记录的道路上现有的车的数量
- private int com=0;//com记录的这条道路上过去了多少车
- private lamp lampp;
- road(String name,lamp lampp)//通过构造函数来标记路线,确定灯的控制权
- {
- this.name=name;
- this.lampp=lampp;
- }
- void cadd()
- {
- int temp=(int)(Math.random()*2);
- this.cum+=temp;//这条路上随机生成1-10的车辆
- if(temp>0)
- {
- System.out.println(name+"的路上新增了"+temp+"量车");
- }
- }
- void cpas()
- {
- if(this.lampp.getcolor()==2)
- {
- if (cum>0)
- {
- this.cum--;
- this.com++;
- System.out.println(name+"的路上通过了1量车");
- }
- }
- }
- public void run()
- {
- int i=0;
- if(i%5==0)
- {
- this.cadd();
- }
- this.cpas();
- }
- void start()
- {
- Timer timer = new Timer();
- timer.schedule(this,1000,10000);
- }
- }
- class lamp
- {
- private String name;
- private int color=1;//color记录颜色,1为红色,2为绿色
- lamp(String name)//通过构造函数来标记路灯
- {
- this.name=name;
- }
- lamp(){}
- void green()
- {
- this.color=2;
- System.out.println(name+"的绿灯亮了");
- }
- void red()
- {
- this.color=1;
- System.out.println(name+"的红灯亮了");
- }
- int getcolor()
- {
- return this.color;
- }
- void setcollor(int color)
- {
- this.color=color;
- }
- }
- class lampctrl extends java.util.TimerTask
- {
- public lamp[] lamps=new lamp[8];
- lampctrl(lamp...lamps)
- {
- for(lamp la:lamps)
- {
- int i=0;
- this.lamps[i]=la;
- i++;
- }
- }
- void changelamp()
- {
- Timer timer = new Timer();
- timer.schedule(this,1000,10000);
- }
- public void run()
- {
- int i=1;//红绿灯一共有亮的情况和顺序 1:N2S和S2N亮.2:S2W和N2E.3:W2E,E2W.4:W2N,E2S
- switch(i)
- {
- case 1:
- if(lamps[7].getcolor()==2)
- {
- lamps[7].red();
- lamps[8].red();
- }
- lamps[1].green();
- lamps[2].green();
- break;
- case 2:
- lamps[1].red();
- lamps[2].red();
- lamps[3].green();
- lamps[4].green();
- break;
- case 3:
- lamps[3].red();
- lamps[4].red();
- lamps[5].green();
- lamps[6].green();
- break;
- case 4:
- lamps[5].red();
- lamps[6].red();
- lamps[7].green();
- lamps[8].green();
- break;
- }
- i++;
- if (i>4){i=1;}
- }
- }
- class roadctrl
- {
- public static void main(String[] args)
- {//红绿灯一共有亮的情况和顺序 1:N2S和S2N亮.2:S2W和N2E.3:W2E,E2W.4:W2N,E2S
- lamp l1=new lamp("N2S");lamp l2=new lamp("S2N");lamp l3=new lamp("S2W");lamp l4=new lamp("N2E");
- lamp l5=new lamp("W2E");lamp l6=new lamp("E2W");lamp l7=new lamp("W2N");lamp l8=new lamp("E2S");
- lampctrl lc=new lampctrl(l1,l2,l3,l4,l5,l6,l7,l8);
- lamp ll=new lamp(); ll.setcollor(2);
- road r1=new road("N2S",l1); road r2=new road("S2N",l2); road r3=new road("S2W",l3); road r4=new road("N2E",l4);
- road r5=new road("N2S",l5); road r6=new road("N2S",l6); road r7=new road("N2S",l7); road r8=new road("N2S",l8);
- road r9=new road("S2E",ll); road r10=new road("E2N",ll); road r11=new road("N2W",ll); road r12=new road("W2S",ll);
- lc.changelamp();
- r1.start();r2.start();r3.start();r4.start();r5.start();r6.start();r7.start();r8.start();r9.start();r10.start();
- r11.start();r12.start();
- }
- }
复制代码 没用多线程,纯Timer控制的,可能错误有点多,请大家都指出来。
这是报错
Exception in thread "Timer-0"
java.lang.NullPointerException
at lampctrl.run(roadctrl.java:101)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source) |