生活随笔
收集整理的這篇文章主要介紹了
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
.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
.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#模拟最简单的交通信号灯的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。