日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

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

發布時間:2024/1/23 C# 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 = "";} }

總結

以上是生活随笔為你收集整理的C#模拟最简单的交通信号灯的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。