界面大致這個樣子:
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 = "";
}
}