程序寫的有點繁雜,但大體功能出來的!
效果圖:

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Threading; 7 8 /* 9 * 空間名:TrafficLight 10 * 功能:模擬交通燈 11 * 編寫人:Doget 12 * 編寫日期:2017.10.14 13 */ 14 namespace TrafficLight 15 { 16 class Program 17 { 18 const int RED_TIME = 10; 19 const int GREEN_TIME = 15; 20 const int YELLOW_TIME = 3; 21 22 static void Main(string[] args) 23 { 24 //紅綠燈程序開始 25 START: 26 Console.SetCursorPosition(0, 0); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 27 Console.Write("請按S鍵開啟交通燈:"); 28 if (InputYellow() == "s") 29 { 30 LightOn("green", GREEN_TIME); 31 Thread.Sleep(500); 32 LightOn("red", RED_TIME); 33 Thread.Sleep(500); 34 LightOn("yellow", YELLOW_TIME); 35 Thread.Sleep(500); 36 } 37 else 38 { 39 Console.WriteLine("輸入錯誤,請重新輸入!"); 40 Console.Clear(); 41 goto START; 42 } 43 Console.Write("按g鍵繼續,其他鍵退出:"); 44 if (InputYellow() == "g") 45 { 46 goto START; 47 } 48 else 49 Console.ReadKey(); 50 } 51 /// <summary> 52 /// 輸入黃色字體的內容 53 /// </summary> 54 /// <returns>用戶輸入的內容</returns> 55 static string InputYellow() 56 { 57 string startCmd; 58 Console.ForegroundColor = ConsoleColor.Yellow; 59 startCmd = (Console.ReadLine()).ToLower(); 60 Console.ResetColor(); 61 return startCmd; 62 } 63 /// <summary> 64 /// 亮燈控制 65 /// </summary> 66 /// <param name="color">設置亮燈的顏色</param> 67 /// <param name="time">設置亮燈的時間</param> 68 static void LightOn(string color, int time) 69 { 70 switch (color.ToLower()) 71 { 72 case "green": 73 Console.ForegroundColor = ConsoleColor.Green; 74 for (int i = time; i > 0; i--) 75 { 76 ShowNumber(i); 77 Thread.Sleep(1000); 78 } 79 Console.ResetColor(); 80 break; 81 case "red": 82 Console.ForegroundColor = ConsoleColor.Red; 83 for (int i = time; i > 0; i--) 84 { 85 if (i < 10) 86 { 87 ShowNumber(i); 88 } 89 Thread.Sleep(1000); 90 } 91 Console.ResetColor(); 92 break; 93 case "yellow": 94 Console.ForegroundColor = ConsoleColor.Yellow; 95 for (int i = time; i > 0; i--) 96 { 97 if (i < 10) 98 { 99 ShowNumber(i); 100 } 101 Thread.Sleep(1000); 102 } 103 Console.ResetColor(); 104 break; 105 default: 106 break; 107 } 108 } 109 /// <summary> 110 /// 顯示數字方法 111 /// </summary> 112 /// <param name="number">要顯示的數字</param> 113 static void ShowNumber(int number) 114 { 115 int geNum = number % 10; 116 int shiNum = number / 10 % 10; 117 int baiNum = number / 10 % 10; 118 119 PrintNum(shiNum,0, 1); 120 PrintNum(geNum,8,1); 121 } 122 /// <summary> 123 /// 控制台打印要顯示的字符 124 /// </summary> 125 /// <param name="number">要打印的數字</param> 126 /// <param name="shifx">打印數字距離屏幕左邊距離</param> 127 /// <param name="shify">打印數字距離屏幕上邊距離</param> 128 static void PrintNum(int number, int shifx, int shify) 129 { 130 switch (number) 131 { 132 case 0: 133 Console.SetCursorPosition(shifx, shify); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 134 Console.Write(" *** \n"); 135 Console.SetCursorPosition(shifx, shify+1); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 136 Console.Write(" * * \n"); 137 Console.SetCursorPosition(shifx, shify+2); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 138 Console.Write("* * \n"); 139 Console.SetCursorPosition(shifx, shify+3); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 140 Console.Write("* * \n"); 141 Console.SetCursorPosition(shifx, shify+4); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 142 Console.Write("* * \n"); 143 Console.SetCursorPosition(shifx, shify+5); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 144 Console.Write(" * * \n"); 145 Console.SetCursorPosition(shifx, shify+6); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 146 Console.Write(" *** \n"); 147 break; 148 case 1: 149 Console.SetCursorPosition(shifx, shify); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 150 Console.Write(" * \n"); 151 Console.SetCursorPosition(shifx, shify+1); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 152 Console.Write(" ** \n"); 153 Console.SetCursorPosition(shifx, shify+2); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 154 Console.Write(" * * \n"); 155 Console.SetCursorPosition(shifx, shify+3); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 156 Console.Write(" * \n"); 157 Console.SetCursorPosition(shifx, shify+4); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 158 Console.Write(" * \n"); 159 Console.SetCursorPosition(shifx, shify+5); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 160 Console.Write(" * \n"); 161 Console.SetCursorPosition(shifx, shify+6); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 162 Console.Write("*******\n"); 163 break; 164 case 2: 165 Console.SetCursorPosition(shifx, shify); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 166 Console.Write("*******\n"); 167 Console.SetCursorPosition(shifx, shify+1); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 168 Console.Write(" *\n"); 169 Console.SetCursorPosition(shifx, shify+2); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 170 Console.Write(" *\n"); 171 Console.SetCursorPosition(shifx, shify+3); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 172 Console.Write("*******\n"); 173 Console.SetCursorPosition(shifx, shify+4); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 174 Console.Write("* \n"); 175 Console.SetCursorPosition(shifx, shify+5); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 176 Console.Write("* \n"); 177 Console.SetCursorPosition(shifx, shify+6); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 178 Console.Write("*******\n"); 179 break; 180 case 3: 181 Console.SetCursorPosition(shifx, shify); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 182 Console.Write("*******\n"); 183 Console.SetCursorPosition(shifx, shify+1); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 184 Console.Write(" *\n"); 185 Console.SetCursorPosition(shifx, shify+2); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 186 Console.Write(" *\n"); 187 Console.SetCursorPosition(shifx, shify+3); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 188 Console.Write("*******\n"); 189 Console.SetCursorPosition(shifx, shify+4); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 190 Console.Write(" *\n"); 191 Console.SetCursorPosition(shifx, shify+5); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 192 Console.Write(" *\n"); 193 Console.SetCursorPosition(shifx, shify+6); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 194 Console.Write("*******\n"); 195 break; 196 case 4: 197 Console.SetCursorPosition(shifx, shify); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 198 Console.Write("* *\n"); 199 Console.SetCursorPosition(shifx, shify+1); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 200 Console.Write("* *\n"); 201 Console.SetCursorPosition(shifx, shify+2); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 202 Console.Write("* *\n"); 203 Console.SetCursorPosition(shifx, shify+3); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 204 Console.Write("*******\n"); 205 Console.SetCursorPosition(shifx, shify+4); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 206 Console.Write(" *\n"); 207 Console.SetCursorPosition(shifx, shify+5); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 208 Console.Write(" *\n"); 209 Console.SetCursorPosition(shifx, shify+6); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 210 Console.Write(" *\n"); 211 break; 212 case 5: 213 Console.SetCursorPosition(shifx, shify); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 214 Console.Write("*******\n"); 215 Console.SetCursorPosition(shifx, shify+1); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 216 Console.Write("* \n"); 217 Console.SetCursorPosition(shifx, shify+2); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 218 Console.Write("* \n"); 219 Console.SetCursorPosition(shifx, shify+3); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 220 Console.Write("*******\n"); 221 Console.SetCursorPosition(shifx, shify+4); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 222 Console.Write(" *\n"); 223 Console.SetCursorPosition(shifx, shify+5); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 224 Console.Write(" *\n"); 225 Console.SetCursorPosition(shifx, shify+6); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 226 Console.Write("*******\n"); 227 break; 228 case 6: 229 Console.SetCursorPosition(shifx, shify); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 230 Console.Write("*******\n"); 231 Console.SetCursorPosition(shifx, shify+1); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 232 Console.Write("* \n"); 233 Console.SetCursorPosition(shifx, shify+2); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 234 Console.Write("* \n"); 235 Console.SetCursorPosition(shifx, shify+3); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 236 Console.Write("*******\n"); 237 Console.SetCursorPosition(shifx, shify+4); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 238 Console.Write("* *\n"); 239 Console.SetCursorPosition(shifx, shify+5); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 240 Console.Write("* *\n"); 241 Console.SetCursorPosition(shifx, shify+6); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 242 Console.Write("*******\n"); 243 break; 244 case 7: 245 Console.SetCursorPosition(shifx, shify); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 246 Console.Write("*******\n"); 247 Console.SetCursorPosition(shifx, shify+1); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 248 Console.Write(" *\n"); 249 Console.SetCursorPosition(shifx, shify+2); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 250 Console.Write(" *\n"); 251 Console.SetCursorPosition(shifx, shify+3); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 252 Console.Write(" *\n"); 253 Console.SetCursorPosition(shifx, shify+4); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 254 Console.Write(" *\n"); 255 Console.SetCursorPosition(shifx, shify+5); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 256 Console.Write(" *\n"); 257 Console.SetCursorPosition(shifx, shify+6); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 258 Console.Write(" *\n"); 259 break; 260 case 8: 261 Console.SetCursorPosition(shifx, shify); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 262 Console.Write(" ***** \n"); 263 Console.SetCursorPosition(shifx, shify+1); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 264 Console.Write("* *\n"); 265 Console.SetCursorPosition(shifx, shify+2); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 266 Console.Write("* *\n"); 267 Console.SetCursorPosition(shifx, shify+3); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 268 Console.Write(" *** \n"); 269 Console.SetCursorPosition(shifx, shify+4); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 270 Console.Write("* *\n"); 271 Console.SetCursorPosition(shifx, shify+5); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 272 Console.Write("* *\n"); 273 Console.SetCursorPosition(shifx, shify+6); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 274 Console.Write(" ***** \n"); 275 break; 276 case 9: 277 Console.SetCursorPosition(shifx, shify); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 278 Console.Write("*******\n"); 279 Console.SetCursorPosition(shifx, shify+1); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 280 Console.Write("* *\n"); 281 Console.SetCursorPosition(shifx, shify+2); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 282 Console.Write("* *\n"); 283 Console.SetCursorPosition(shifx, shify+3); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 284 Console.Write("*******\n"); 285 Console.SetCursorPosition(shifx, shify+4); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 286 Console.Write(" *\n"); 287 Console.SetCursorPosition(shifx, shify+5); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 288 Console.Write(" *\n"); 289 Console.SetCursorPosition(shifx, shify+6); //設置光標位置離屏幕左邊為0,離屏幕頂部為1 290 Console.Write("*******\n"); 291 break; 292 default: 293 break; 294 } 295 } 296 } 297 }