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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 你为谁归来 中级黑马   /  2014-5-7 20:43  /  1186 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

按照自己的思想去做的交通灯的代码,很乱,而且没有注释(大家以后写代码要引以为戒,因为我自己都快看不懂啦!哈哈!),因为以前是学C的,所以比较偏向面向过程,请大家点评下!轻点喷啊!


  1. public class Traffic1
  2. {

  3.         public static void main(String[] args) throws Exception
  4.         {
  5.                 new Vehicle1(100);

  6.         }

  7. }
  8. class Operation implements Runnable
  9. {
  10.         public  ArrayList<String> jie ;

  11.         public Operation(ArrayList<String> jie)
  12.         {
  13.                 this.jie=jie;
  14.         }
  15.         public void run()
  16.         {
  17.                 if(jie.size()>10)
  18.                 {
  19.                         for (int i = 0; i < 10; i++)
  20.                         {
  21.                                 try
  22.                                 {
  23.                                         Thread.sleep(1000);
  24.                                 } catch (Exception e)
  25.                                 {
  26.                                         // TODO Auto-generated catch block
  27.                                         e.printStackTrace();
  28.                                 }
  29.                                 jie.remove(i);
  30.                                 System.out.println("走了一辆车");
  31.                         }
  32.                        
  33.                 }else
  34.                 {
  35.                         for(int i =0;i<jie.size();i++)
  36.                         {
  37.                                 try
  38.                                 {
  39.                                         Thread.sleep(1000);
  40.                                 } catch (Exception e)
  41.                                 {
  42.                                         // TODO Auto-generated catch block
  43.                                         e.printStackTrace();
  44.                                 }
  45.                                 jie.remove(i);
  46.                                 System.out.println("走了一辆车");
  47.                         }
  48.                 }
  49.                
  50.         }
  51.        

  52. }


  53. class Control
  54. {
  55.        
  56.         public void Connection1() throws Exception
  57.         {
  58.                 new Thread(new Operation(Vehicle1.al1)).start();
  59.                 new Thread(new Operation(Vehicle1.al2)).start();
  60.                 System.out.println("南北-北南-南东-北西-方向的绿灯亮了,请车辆过往。");
  61.                 Thread.sleep(10000);
  62.                 Connection2();
  63.         }
  64.         public void Connection2() throws Exception
  65.         {
  66.                 new Thread(new Operation(Vehicle1.al3)).start();
  67.                 new Thread(new Operation(Vehicle1.al4)).start();
  68.                 System.out.println("南西-北东-方向的绿灯亮了,请车辆过往。");
  69.                 Thread.sleep(10000);
  70.                 Connection3();
  71.         }
  72.         public void Connection3() throws Exception
  73.         {
  74.                
  75.                 new Thread(new Operation(Vehicle1.al5)).start();
  76.                 new Thread(new Operation(Vehicle1.al6)).start();
  77.                 System.out.println("西东-东西-西南-东北-方向的绿灯亮了,请车辆过往。");
  78.                 Thread.sleep(10000);
  79.                 Connection4();
  80.         }
  81.         public void Connection4() throws Exception
  82.         {
  83.                 new Thread(new Operation(Vehicle1.al7)).start();
  84.                 new Thread(new Operation(Vehicle1.al8)).start();
  85.                 System.out.println("西北-东南-方向的绿灯亮了,请车辆过往。");
  86.                 Thread.sleep(10000);
  87.                 Connection1();
  88.         }
  89.        

  90. }


  91. class Vehicle1
  92. {
  93.         public int x;
  94.         public static ArrayList<String> al1,al2,al3,al4,al5,al6,al7,al8;
  95.         public Vehicle1(int x) throws Exception
  96.         {
  97.                 this.x=x;
  98.                 al1 = new ArrayList<String>();
  99.                 al2 = new ArrayList<String>();
  100.                 al3 = new ArrayList<String>();
  101.                 al4 = new ArrayList<String>();
  102.                 al5 = new ArrayList<String>();
  103.                 al6 = new ArrayList<String>();
  104.                 al7 = new ArrayList<String>();
  105.                 al8 = new ArrayList<String>();
  106.                 new Thread(new Light1(x)).start();
  107.                 new Control().Connection1();
  108.                
  109.                
  110.         }

  111. }

  112. class Light1 implements Runnable
  113. {
  114.         public int o1;
  115.         public int o2=0;
  116.         public Light1(int o1)
  117.         {
  118.                 this.o1=o1;
  119.         }
  120.         public int Random()
  121.         {
  122.                 int o = (int) (Math.random()*12+1);
  123.                 return o;
  124.         }

  125.         public void run()
  126.         {

  127.                 while(o2<o1)
  128.                 {
  129.                         try
  130.                         {
  131.                                 Thread.sleep((new Random().nextInt(10)+1)*1000);
  132.                         } catch (Exception e)
  133.                         {
  134.                                 // TODO Auto-generated catch block
  135.                                 e.printStackTrace();
  136.                         }
  137.                         switch(Random())
  138.                         {
  139.                                 case 1: Vehicle1.al1.add("che");
  140.                                                 System.out.println("从南到北来了一辆车");
  141.                                         break;
  142.                                 case 2: Vehicle1.al2.add("che");
  143.                                                 System.out.println("从北到南来了一辆车");
  144.                                         break;
  145.                                 case 3: Vehicle1.al3.add("che");
  146.                                                 System.out.println("从南到西来了一辆车");
  147.                                         break;
  148.                                 case 4: Vehicle1.al4.add("che");
  149.                                                 System.out.println("从北到东来了一辆车");
  150.                                         break;
  151.                                 case 5: Vehicle1.al5.add("che");
  152.                                                 System.out.println("从西到东来了一辆车");
  153.                                         break;
  154.                                 case 6: Vehicle1.al6.add("che");
  155.                                                 System.out.println("从东到西来了一辆车");
  156.                                         break;
  157.                                 case 7: Vehicle1.al7.add("che");
  158.                                                 System.out.println("从西到北来了一辆车");
  159.                                         break;
  160.                                 case 8: Vehicle1.al8.add("che");
  161.                                                 System.out.println("从东到南来了一辆车");
  162.                                         break;
  163.                                 case 9:Vehicle1. al1.add("che");
  164.                                                 System.out.println("从南到东来了一辆车");
  165.                                         break;
  166.                                 case 10:Vehicle1. al2.add("che");
  167.                                                 System.out.println("从北到西来了一辆车");
  168.                                         break;
  169.                                 case 11: Vehicle1.al5.add("che");
  170.                                                 System.out.println("从西到南来了一辆车");
  171.                                         break;
  172.                                 case 12: Vehicle1.al6.add("che");
  173.                                                 System.out.println("从东到北来了一辆车");
  174.                                         break;
  175.                         }
  176.                         o2++;
  177.                 }
  178.                
  179.         }
  180. }
复制代码

3 个回复

倒序浏览
这么厉害.这视频我都还没听呢
回复 使用道具 举报
好一个面向过程。
回复 使用道具 举报
不断学习,不断成长
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马