《实时控制软件设计》第二周作业
停车场门禁系统状态机:
代码如下:
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(); } } } }
运行结果如下:
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步