停车场门禁系统状态机:
代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace qichemenjin { /// <summary> /// 判断起降杆状态 /// </summary> class LiftLever { public bool llstate; public LiftLever() { llstate = false; } public void Showllstate() { if (llstate == true) Console.WriteLine("起落杆升"); else Console.WriteLine("起落杆降"); } } /// <summary> /// 判断等的状态 /// </summary> class TrafficLight { public bool tlstate; public TrafficLight() { tlstate = false; } public void Showtlstate() { if (tlstate == true) Console.WriteLine("绿灯亮"); else Console.WriteLine("红灯亮"); } } /// <summary> /// 判断车的状态 /// </summary> class Car { public bool carstate; public Car() { carstate = false; } public void Showcarstate() { if (carstate == true) Console.WriteLine("有车入闸"); else Console.WriteLine("车已出闸"); } } class Program { static void Main(string[] args) { Car car = new Car(); LiftLever lever = new LiftLever(); TrafficLight light = new TrafficLight(); car.Showcarstate(); lever.Showllstate(); light.Showtlstate(); while (car.carstate=true) { Console.WriteLine("是否有车入闸(y/n)"); var input = Console.Read(); if(input=='y') { car.carstate = true; car.Showcarstate(); lever.llstate = true; lever.Showllstate(); light.tlstate = true; light.Showtlstate(); Console.Read(); } else { Console.WriteLine("车已出闸"); Console.WriteLine("起落杆降"); Console.WriteLine("红灯亮"); Console.Read(); } Console.Read(); } } } }
运行结果如下: