黑马程序员技术交流社区
标题:
自己写的交通灯代码
[打印本页]
作者:
你为谁归来
时间:
2014-5-7 20:43
标题:
自己写的交通灯代码
按照自己的思想去做的交通灯的代码,很乱,而且没有注释(大家以后写代码要引以为戒,因为我自己都快看不懂啦!哈哈!),因为以前是学C的,所以比较偏向面向过程,请大家点评下!轻点喷啊!
public class Traffic1
{
public static void main(String[] args) throws Exception
{
new Vehicle1(100);
}
}
class Operation implements Runnable
{
public ArrayList<String> jie ;
public Operation(ArrayList<String> jie)
{
this.jie=jie;
}
public void run()
{
if(jie.size()>10)
{
for (int i = 0; i < 10; i++)
{
try
{
Thread.sleep(1000);
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
jie.remove(i);
System.out.println("走了一辆车");
}
}else
{
for(int i =0;i<jie.size();i++)
{
try
{
Thread.sleep(1000);
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
jie.remove(i);
System.out.println("走了一辆车");
}
}
}
}
class Control
{
public void Connection1() throws Exception
{
new Thread(new Operation(Vehicle1.al1)).start();
new Thread(new Operation(Vehicle1.al2)).start();
System.out.println("南北-北南-南东-北西-方向的绿灯亮了,请车辆过往。");
Thread.sleep(10000);
Connection2();
}
public void Connection2() throws Exception
{
new Thread(new Operation(Vehicle1.al3)).start();
new Thread(new Operation(Vehicle1.al4)).start();
System.out.println("南西-北东-方向的绿灯亮了,请车辆过往。");
Thread.sleep(10000);
Connection3();
}
public void Connection3() throws Exception
{
new Thread(new Operation(Vehicle1.al5)).start();
new Thread(new Operation(Vehicle1.al6)).start();
System.out.println("西东-东西-西南-东北-方向的绿灯亮了,请车辆过往。");
Thread.sleep(10000);
Connection4();
}
public void Connection4() throws Exception
{
new Thread(new Operation(Vehicle1.al7)).start();
new Thread(new Operation(Vehicle1.al8)).start();
System.out.println("西北-东南-方向的绿灯亮了,请车辆过往。");
Thread.sleep(10000);
Connection1();
}
}
class Vehicle1
{
public int x;
public static ArrayList<String> al1,al2,al3,al4,al5,al6,al7,al8;
public Vehicle1(int x) throws Exception
{
this.x=x;
al1 = new ArrayList<String>();
al2 = new ArrayList<String>();
al3 = new ArrayList<String>();
al4 = new ArrayList<String>();
al5 = new ArrayList<String>();
al6 = new ArrayList<String>();
al7 = new ArrayList<String>();
al8 = new ArrayList<String>();
new Thread(new Light1(x)).start();
new Control().Connection1();
}
}
class Light1 implements Runnable
{
public int o1;
public int o2=0;
public Light1(int o1)
{
this.o1=o1;
}
public int Random()
{
int o = (int) (Math.random()*12+1);
return o;
}
public void run()
{
while(o2<o1)
{
try
{
Thread.sleep((new Random().nextInt(10)+1)*1000);
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
switch(Random())
{
case 1: Vehicle1.al1.add("che");
System.out.println("从南到北来了一辆车");
break;
case 2: Vehicle1.al2.add("che");
System.out.println("从北到南来了一辆车");
break;
case 3: Vehicle1.al3.add("che");
System.out.println("从南到西来了一辆车");
break;
case 4: Vehicle1.al4.add("che");
System.out.println("从北到东来了一辆车");
break;
case 5: Vehicle1.al5.add("che");
System.out.println("从西到东来了一辆车");
break;
case 6: Vehicle1.al6.add("che");
System.out.println("从东到西来了一辆车");
break;
case 7: Vehicle1.al7.add("che");
System.out.println("从西到北来了一辆车");
break;
case 8: Vehicle1.al8.add("che");
System.out.println("从东到南来了一辆车");
break;
case 9:Vehicle1. al1.add("che");
System.out.println("从南到东来了一辆车");
break;
case 10:Vehicle1. al2.add("che");
System.out.println("从北到西来了一辆车");
break;
case 11: Vehicle1.al5.add("che");
System.out.println("从西到南来了一辆车");
break;
case 12: Vehicle1.al6.add("che");
System.out.println("从东到北来了一辆车");
break;
}
o2++;
}
}
}
复制代码
作者:
倪大大
时间:
2014-5-7 21:14
这么厉害.这视频我都还没听呢
作者:
轻语。
时间:
2014-5-7 21:35
好一个面向过程。
作者:
吉大人
时间:
2014-5-7 21:38
不断学习,不断成长
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2