一、需求分析
通过控制台输入绿灯,黄灯,红灯显示时间,然后倒计时,周而复始。
二、运行效果
1、输入显示时间
2、绿灯,黄灯,红灯,倒计时周而复始
三、实现代码
![](https://image.shishitao.com:8440/aHR0cHM6Ly9pbWFnZXMuY25ibG9ncy5jb20vT3V0bGluaW5nSW5kaWNhdG9ycy9Db250cmFjdGVkQmxvY2suZ2lm.gif?w=700&webp=1)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading; 6 using System.Threading.Tasks; 7 8 namespace ConsoleApp1 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 Console.ForegroundColor = ConsoleColor.Green; 15 Console.Write("请输入绿灯显示时间:"); 16 int GreenNum = InputNum(); 17 18 Console.ForegroundColor = ConsoleColor.Yellow; 19 Console.Write("请输入黄灯显示时间:"); 20 int YellowNum = InputNum(); 21 22 Console.ForegroundColor = ConsoleColor.Red; 23 Console.Write("请输入红灯显示时间:"); 24 int RedNum = InputNum(); 25 //循环执行 26 while (true) 27 { 28 //绿灯倒计时 29 for (int i = GreenNum; i > 0; i--) 30 { 31 WriteNum(i, ConsoleColor.Green); 32 //暂停1秒 33 Thread.Sleep(1000); 34 } 35 //黄灯倒计时 36 for (int i = YellowNum; i > 0; i--) 37 { 38 WriteNum(i, ConsoleColor.Yellow); 39 //暂停1秒 40 Thread.Sleep(1000); 41 } 42 //红灯倒计时 43 for (int i = RedNum; i > 0; i--) 44 { 45 WriteNum(i, ConsoleColor.Red); 46 //暂停1秒 47 Thread.Sleep(1000); 48 } 49 } 50 } 51 /// <summary> 52 /// 获取输入值逻辑 53 /// </summary> 54 /// <returns></returns> 55 public static int InputNum() 56 { 57 string s_Num = Console.ReadLine(); 58 if(s_Num.Length>2) 59 { 60 Console.Write("输入值不能超过两位数,请重新输入:"); 61 return InputNum(); 62 } 63 int Num = 0; 64 bool IsNum = int.TryParse(s_Num, out Num); 65 if(!IsNum) 66 { 67 Console.Write("输入值不是整数,请重新输入:"); 68 return InputNum(); 69 } 70 if(Num<=0) 71 { 72 Console.Write("输入值不是正数,请重新输入:"); 73 return InputNum(); 74 } 75 if (Num > 99 ) 76 { 77 Console.Write("输入值不能超过99,请重新输入:"); 78 return InputNum(); 79 } 80 return Num; 81 } 82 /// <summary> 83 /// 生成指定数字 84 /// </summary> 85 /// <param name="Num"></param> 86 public static void WriteNum(int Num, ConsoleColor consoleColor) 87 { 88 int FirstNum = 0; 89 int SecondNum = Num; 90 if (Num.ToString().Length == 2) 91 { 92 FirstNum = int.Parse(Num.ToString()[0].ToString()); 93 SecondNum = int.Parse(Num.ToString()[1].ToString()); 94 } 95 Console.Clear(); 96 for (int i = 0; i < 20; i++) 97 { 98 for (int j = 0; j < 10; j++) 99 { 100 ChangeColor(FirstNum, i, j, consoleColor); 101 Console.Write("●"); 102 } 103 Console.Write(" "); 104 for (int j = 0; j < 10; j++) 105 { 106 ChangeColor(SecondNum, i, j, consoleColor); 107 Console.Write("●"); 108 } 109 Console.WriteLine(); 110 } 111 } 112 public static void ChangeColor(int Num, int i, int j, ConsoleColor consoleColor) 113 { 114 Console.ForegroundColor = ConsoleColor.White; 115 switch (Num) 116 { 117 case 0: 118 if (i <= 1 || i >= 18 || j <= 1 || j >= 8) 119 { 120 Console.ForegroundColor = consoleColor; 121 } 122 break; 123 case 1: 124 if (j >= 8) 125 { 126 Console.ForegroundColor = consoleColor; 127 } 128 break; 129 case 2: 130 if (i <= 1 || i == 9 || i == 10 || i >= 18) 131 { 132 Console.ForegroundColor = consoleColor; 133 } 134 if (i < 10 && j >= 8) 135 { 136 Console.ForegroundColor = consoleColor; 137 } 138 if (i > 10 && j <= 1) 139 { 140 Console.ForegroundColor = consoleColor; 141 } 142 break; 143 case 3: 144 if (i == 0 || i == 1 || i == 9 || i == 10 || i == 18 || i == 19) 145 { 146 Console.ForegroundColor = consoleColor; 147 } 148 if (j >= 8) 149 { 150 Console.ForegroundColor = consoleColor; 151 } 152 break; 153 case 4: 154 if (i == 9 || i == 10) 155 { 156 Console.ForegroundColor = consoleColor; 157 } 158 if (i < 10 && j <= 1) 159 { 160 Console.ForegroundColor = consoleColor; 161 } 162 if (j >= 8) 163 { 164 Console.ForegroundColor = consoleColor; 165 } 166 break; 167 case 5: 168 if (i <= 1 || i == 9 || i == 10 || i >= 18) 169 { 170 Console.ForegroundColor = consoleColor; 171 } 172 if (i > 10 && j >= 8) 173 { 174 Console.ForegroundColor = consoleColor; 175 } 176 if (i < 10 && j <= 1) 177 { 178 Console.ForegroundColor = consoleColor; 179 } 180 break; 181 case 6: 182 if (i <= 1 || i == 9 || i == 10 || i >= 18) 183 { 184 Console.ForegroundColor = consoleColor; 185 } 186 if (j <= 1) 187 { 188 Console.ForegroundColor = consoleColor; 189 } 190 if (i > 10 && j >= 8) 191 { 192 Console.ForegroundColor = consoleColor; 193 } 194 break; 195 case 7: 196 if (i <= 1) 197 { 198 Console.ForegroundColor = consoleColor; 199 } 200 if (j >= 8) 201 { 202 Console.ForegroundColor = consoleColor; 203 } 204 break; 205 case 8: 206 if (i <= 1 || i >= 18 || i == 9 || i == 10) 207 { 208 Console.ForegroundColor = consoleColor; 209 } 210 if (j <= 1 || j >= 8) 211 { 212 Console.ForegroundColor = consoleColor; 213 } 214 break; 215 case 9: 216 if (i <= 1 || i == 9 || i == 10 || i >= 18) 217 { 218 Console.ForegroundColor = consoleColor; 219 } 220 if (i < 10 && j <= 1) 221 { 222 Console.ForegroundColor = consoleColor; 223 } 224 if (j >= 8) 225 { 226 Console.ForegroundColor = consoleColor; 227 } 228 break; 229 } 230 } 231 } 232 }
注:以上代码属个人整理,用于交流学习,原创。转载请标明出处。(QQ/微信:742010299 昵称:同心同德)