A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 翟友伟 黑马帝   /  2012-3-30 16:35  /  1702 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

按照视频上的代码来打。。 不知道哪里有问题
最后输入结果,  当前绿灯转换不过来, 总是固定停在一个   上  ,
提示为 :
S2N为绿灯 ------------一开始的没问题
N2S为绿灯
-------------------------
----------------------
--------------
-------------------------
----------------------
--------------

绿灯从"S2N"----切换为S2W

以后无论继续运行    一直显示    “绿灯从"S2N"----切换为S2W ”

贴上代码,,,哪位同学帮我看看 哪里出问题了。。。谢谢l了



Lamp类
  1. package com.itcast;

  2. public enum Lamp {
  3.         S2N("N2S","S2W",false),S2W("N2S","S2W",false),
  4.         E2W("W2E","E2S",false),E2S("W2N","S2N",false),
  5.        
  6.         S2E(null,null,true),E2N(null,null,true),
  7.         N2W(null,null,true),W2S(null,null,true),
  8.         N2S(null,null,false),N2E(null,null,false),
  9.         W2E(null,null,false),W2N(null,null,false);
  10.        
  11.        
  12.        
  13.         private String  opposite;
  14.         private boolean lighted;
  15.         private String next;
  16.        
  17.         private Lamp(String opposite,String next,boolean lighted)
  18.         {
  19.                 this.opposite=opposite;
  20.                 this.next=next;
  21.                 this.lighted=lighted;
  22.                
  23.                
  24.         }
  25.         public void light()
  26.         {
  27.                 this.lighted=true;
  28.                 if(opposite!=null)
  29.                 {
  30.                         Lamp.valueOf(opposite).light();
  31.                        
  32.                 }
  33.                 System.out.println(name()+"绿了");  
  34.         }

  35.         public boolean isLighted()
  36.         {
  37.                 return lighted;
  38.         }
  39.        
  40.         public Lamp blackOut()
  41.         {
  42.                 this.lighted=false;
  43.                 if(opposite!=null)
  44.                 {
  45.                         Lamp lampOpposite =Lamp.valueOf(opposite);
  46.                         lampOpposite.blackOut();
  47.                 }
  48.                
  49.                 Lamp nextLamp =null;
  50.                 if(next!=null)
  51.                 {
  52.                        
  53.                         nextLamp=Lamp.valueOf(next);
  54.                         System.out.println("绿灯从"+name()+"----切换为"+next);
  55.                         nextLamp.light();
  56.                 }
  57.                 return nextLamp;
  58.         }       
  59. }
复制代码
LampController类
  1. public class LampController
  2. {
  3.        
  4.         private Lamp currentLamp ;
  5.         public  LampController()
  6.         {
  7.                 currentLamp =Lamp.S2N;               
  8.        
  9.                 currentLamp.light();
  10.                 ScheduledExecutorService timer =  Executors.newScheduledThreadPool(1);
  11.                 timer.scheduleAtFixedRate(
  12.                         new Runnable()
  13.                         {
  14.                                 public  void run()
  15.                                         {                                               
  16.                                                 currentLamp = currentLamp.blackOut();
  17.                                         }
  18.                         },
  19.                         6,6,TimeUnit.SECONDS);
  20.                
  21.         }
  22. }
复制代码
Road类
  1. public class Road {
  2.         private List<String> vechicles = new ArrayList<String>();
  3.         private String name =null; ;
  4.         public Road (String name)
  5.         {
  6.                 this.name = name;
  7.                 ExecutorService pool =  Executors.newSingleThreadExecutor();
  8.                 pool.execute(new Runnable(){
  9.                         public void run()
  10.                         {
  11.                                 for (int i=1;i<1000;i++)
  12.                                 {
  13.                                         try{
  14.                                                 Thread.sleep((new Random().nextInt(10)+1)*1000);
  15.                                                 }
  16.                                         catch(InterruptedException e)
  17.                                                 {e.printStackTrace();}
  18.                                         vechicles.add(Road.this.name+"_"+i);
  19.                                 }
  20.                         }
  21.                 });
  22.         ScheduledExecutorService timer =Executors.newScheduledThreadPool(1);
  23.         timer.scheduleAtFixedRate(
  24.                         new  Runnable(){
  25.                 public void run()
  26.                 {
  27.                         if(vechicles.size()>0)
  28.                         {
  29.                                 boolean lighted= Lamp.valueOf(Road.this.name).isLighted();
  30.                                         if(lighted)
  31.                                         {
  32.                                                 System.out.println(vechicles.remove(0)+"      is traversing!");
  33.                                         }
  34.                         }
  35.                 }},
  36.                         1,
  37.                         1,
  38.                         TimeUnit.SECONDS);
  39.         }
  40. }
复制代码
MainClass类
  1. public static void main(String[] args)
  2.         {       
  3.                         String [] directions = new String[]{
  4.                                         "S2N","S2W","E2W","E2S","N2S","N2E","W2E","W2N","S2E","E2N","N2W","W2S"               
  5.                         };
  6.                         for(int i=0;i<directions.length;i++){
  7.                                 new Road(directions[i]);
  8.                         }
  9.                         new LampController();

  10.         }

复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马