C#模拟最简单的交通信号灯

界面大致这个样子:
在这里插入图片描述


while循环触发交通灯,交通灯触发汽车的行为。
红绿灯5s,黄灯3s。

代码是:console控制台的!!!!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace 交通信号灯模型
{
    class Program
    {
        public static string light = "";
        public static string car = "";

        static void Main(string[] args)
        {
            Console.WriteLine("交通信号灯开启" + "\r\n");
          
            //交通信号灯变化
            while (true)
            {
                try
                {
                    RedLight();//灯触发
                    CarDo();//车执行
                    System.Threading.Thread.Sleep(5000);//延时

                    YellowLight();
                    CarDo();
                    System.Threading.Thread.Sleep(3000);

                    GreenLight();
                    CarDo();
                    System.Threading.Thread.Sleep(5000);

                    YellowLight();
                    CarDo();
                    System.Threading.Thread.Sleep(3000);

                    Console.WriteLine();
                }
                catch (Exception e)
                {
                    throw;
                    //Console.WriteLine(e.ToString());
                }
                
            }

            Console.ReadKey();
        }

        public static void RedLight()
        {
            Console.Write("红灯" + "  ");
            light = "red";
            
        }
        public static void GreenLight()
        {
            Console.Write("绿灯" + "  ");
            light = "green";
        }
        public static void YellowLight()
        {
            Console.Write("黄灯" + "  ");
            light = "yellow";
        }

        public static void CarDo()
        {
            if (light == "red")
            {
                Console.WriteLine("Car stop");
            }
            else if (light == "green")
            {
                Console.WriteLine("Car go");
            }
            else if (light == "yellow")
            {
                Console.WriteLine("Car maintain");
            }
        }
            
    }
}


升级版本:
1、面向对象封装下,使用了接口
2、增加了东西走向的交通灯和车
3、增加了行人

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace 交通信号灯模型
{
    class Program
    {
        public static string car = "";

        static void Main(string[] args)
        {
            Console.WriteLine("交通信号灯开启" + "\r\n");       //用线程实现有问题,面向对象的设计      //如何把模板类的东西抽象处理呢?

            LightSN lightSN = new LightSN();//南北交通灯           
            LightEW lightEW = new LightEW();//东西交通灯
            Car car = new Car();            //汽车
            People people = new People();   //行人

            //交通信号灯变化
            while (true)
            {
                try
                {                            
                    //南北
                    lightSN.RedLight();
                    car.CarDo();
                    people.PeopleDo();
                    //东西
                    lightEW.GreenLight();
                    car.CarDoEW();
                    people.PeopleEW();
                    System.Threading.Thread.Sleep(10000);//延时

                    //南北
                    lightSN.YellowLight();
                    car.CarDo();
                    people.PeopleDo();
                    //东西
                    lightEW.YellowLight();
                    car.CarDoEW();
                    people.PeopleEW();
                    System.Threading.Thread.Sleep(3000);//延时

                    //南北
                    lightSN.GreenLight();
                    car.CarDo();
                    people.PeopleDo();
                    //东西
                    lightEW.RedLight();
                    car.CarDoEW();
                    people.PeopleEW();
                    System.Threading.Thread.Sleep(10000);//延时

                    //南北
                    lightSN.YellowLight();
                    car.CarDo();
                    people.PeopleDo();
                    //东西
                    lightEW.YellowLight();
                    car.CarDoEW();
                    people.PeopleEW();
                    System.Threading.Thread.Sleep(3000);//延时

                    Console.WriteLine();
                }
                catch (Exception e)
                {
                    throw;
                    //Console.WriteLine(e.ToString());
                }
                
            }

            Console.ReadKey();
        }
           
    }

    internal interface ILight              //东西和南北方向灯的接口
    {
        void RedLight();
        void GreenLight();
        void YellowLight();     
    }
    internal interface IMoveThing       //人和各种汽车的接口
    {
        void MoveTingDo();
    }

    //实现东西和南北方向交通灯类
    public class LightSN : ILight
    {
        public void RedLight()
        {
            Console.WriteLine("南北红灯" + "  ");
            PublicModule.light = "red";
        }
        public void GreenLight()
        {
            Console.WriteLine("南北绿灯" + "  ");
            PublicModule.light = "green";
        }
        public void YellowLight()
        {
            Console.WriteLine("南北黄灯" + "  ");
            PublicModule.light = "yellow";
        }
    }
    public class LightEW : ILight
    {
        public void RedLight()
        {
            Console.WriteLine("东西红灯" + "  ");
            PublicModule.lightwe = "red";

        }
        public void GreenLight()
        {
            Console.WriteLine("东西绿灯" + "  ");
            PublicModule.lightwe = "green";
        }
        public void YellowLight()
        {
            Console.WriteLine("东西黄灯" + "  ");
            PublicModule.lightwe = "yellow";
        }
    }
    //实现汽车和人的类
    public class Car : IMoveThing
    {
        public void MoveTingDo()
        {
        }
        public void CarDo()
        {
            if (PublicModule.light == "red")
            {
                Console.WriteLine("Car stop");
            }
            else if (PublicModule.light == "green")
            {
                Console.WriteLine("Car go");
            }
            else if (PublicModule.light == "yellow")
            {
                Console.WriteLine("Car maintain");
            }
        }
        public void CarDoEW()
        {
            if (PublicModule.lightwe == "red")
            {
                Console.WriteLine("Car stop");
            }
            else if (PublicModule.lightwe == "green")
            {
                Console.WriteLine("Car go");
            }
            else if (PublicModule.lightwe == "yellow")
            {
                Console.WriteLine("Car maintain");
            }
        }
    }
    public class People : IMoveThing
    {
        public void MoveTingDo()
        {
        }
        public void PeopleDo()
        {
            if (PublicModule.light == "red")
            {
                Console.WriteLine("People stop");
            }
            else if (PublicModule.light == "green")
            {
                Console.WriteLine("People go");
            }
            else if (PublicModule.light == "yellow")
            {
                Console.WriteLine("People maintain");
            }
        }
        public void PeopleEW()
        {
            if (PublicModule.lightwe == "red")
            {
                Console.WriteLine("People stop");
            }
            else if (PublicModule.lightwe == "green")
            {
                Console.WriteLine("People go");
            }
            else if (PublicModule.lightwe == "yellow")
            {
                Console.WriteLine("People maintain");
            }
        }
    }
    public class Bus : IMoveThing
    {
        public void MoveTingDo()
        { }
    }
}

调用的公共类



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 交通信号灯模型
{
    class PublicModule
    {
        public static string light = "";
        public static string lightwe = "";
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章