asp.net C# 控制台模拟红绿灯(交通信号灯)

2021-05-17 20:29

阅读:502

标签:不能   执行   inpu   yellow   while   name   OLE   returns   readline   

一、需求分析

通过控制台输入绿灯,黄灯,红灯显示时间,然后倒计时,周而复始。

二、运行效果

1、输入显示时间

技术图片

2、绿灯,黄灯,红灯,倒计时周而复始

技术图片

技术图片技术图片

三、实现代码

技术图片技术图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write("请输入绿灯显示时间:");
            int GreenNum = InputNum();

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Write("请输入黄灯显示时间:");
            int YellowNum = InputNum();

            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write("请输入红灯显示时间:");
            int RedNum = InputNum();
            //循环执行
            while (true)
            {
                //绿灯倒计时
                for (int i = GreenNum; i > 0; i--)
                {
                    WriteNum(i, ConsoleColor.Green);
                    //暂停1秒
                    Thread.Sleep(1000);
                }
                //黄灯倒计时
                for (int i = YellowNum; i > 0; i--)
                {
                    WriteNum(i, ConsoleColor.Yellow);
                    //暂停1秒
                    Thread.Sleep(1000);
                }
                //红灯倒计时
                for (int i = RedNum; i > 0; i--)
                {
                    WriteNum(i, ConsoleColor.Red);
                    //暂停1秒
                    Thread.Sleep(1000);
                }
            }
        }
        /// 
        /// 获取输入值逻辑
        /// 
        /// 
        public static int InputNum()
        {
            string s_Num = Console.ReadLine();
            if(s_Num.Length>2)
            {
                Console.Write("输入值不能超过两位数,请重新输入:");
                return InputNum();
            }
            int Num = 0;
            bool IsNum = int.TryParse(s_Num, out Num);
            if(!IsNum)
            {
                Console.Write("输入值不是整数,请重新输入:");
                return InputNum();
            }
            if(Num0)
            {
                Console.Write("输入值不是正数,请重新输入:");
                return InputNum();
            }
            if (Num > 99 )
            {
                Console.Write("输入值不是超过99,请重新输入:");
                return InputNum();
            }
            return Num;
        }
        /// 
        /// 生成指定数字
        /// 
        /// 
        public static void WriteNum(int Num, ConsoleColor consoleColor)
        {
            int FirstNum = 0;
            int SecondNum = Num;
            if (Num.ToString().Length == 2)
            {
                FirstNum = int.Parse(Num.ToString()[0].ToString());
                SecondNum = int.Parse(Num.ToString()[1].ToString());
            }
            Console.Clear();
            for (int i = 0; i 20; i++)
            {
                for (int j = 0; j 10; j++)
                {
                    ChangeColor(FirstNum, i, j, consoleColor);
                    Console.Write("");
                }
                Console.Write("   ");
                for (int j = 0; j 10; j++)
                {
                    ChangeColor(SecondNum, i, j, consoleColor);
                    Console.Write("");
                }
                Console.WriteLine();
            }
        }
        public static void ChangeColor(int Num, int i, int j, ConsoleColor consoleColor)
        {
            Console.ForegroundColor = ConsoleColor.White;
            switch (Num)
            {
                case 0:
                    if (i 1 || i >= 18 || j 1 || j >= 8)
                    {
                        Console.ForegroundColor = consoleColor;
                    }
                    break;
                case 1:
                    if (j >= 8)
                    {
                        Console.ForegroundColor = consoleColor;
                    }
                    break;
                case 2:
                    if (i 1 || i == 9 || i == 10 || i >= 18)
                    {
                        Console.ForegroundColor = consoleColor;
                    }
                    if (i 10 && j >= 8)
                    {
                        Console.ForegroundColor = consoleColor;
                    }
                    if (i > 10 && j 1)
                    {
                        Console.ForegroundColor = consoleColor;
                    }
                    break;
                case 3:
                    if (i == 0 || i == 1 || i == 9 || i == 10 || i == 18 || i == 19)
                    {
                        Console.ForegroundColor = consoleColor;
                    }
                    if (j >= 8)
                    {
                        Console.ForegroundColor = consoleColor;
                    }
                    break;
                case 4:
                    if (i == 9 || i == 10)
                    {
                        Console.ForegroundColor = consoleColor;
                    }
                    if (i 10 && j 1)
                    {
                        Console.ForegroundColor = consoleColor;
                    }
                    if (j >= 8)
                    {
                        Console.ForegroundColor = consoleColor;
                    }
                    break;
                case 5:
                    if (i 1 || i == 9 || i == 10 || i >= 18)
                    {
                        Console.ForegroundColor = consoleColor;
                    }
                    if (i > 10 && j >= 8)
                    {
                        Console.ForegroundColor = consoleColor;
                    }
                    if (i 10 && j 1)
                    {
                        Console.ForegroundColor = consoleColor;
                    }
                    break;
                case 6:
                    if (i 1 || i == 9 || i == 10 || i >= 18)
                    {
                        Console.ForegroundColor = consoleColor;
                    }
                    if (j 1)
                    {
                        Console.ForegroundColor = consoleColor;
                    }
                    if (i > 10 && j >= 8)
                    {
                        Console.ForegroundColor = consoleColor;
                    }
                    break;
                case 7:
                    if (i 1)
                    {
                        Console.ForegroundColor = consoleColor;
                    }
                    if (j >= 8)
                    {
                        Console.ForegroundColor = consoleColor;
                    }
                    break;
                case 8:
                    if (i 1 || i >= 18 || i == 9 || i == 10)
                    {
                        Console.ForegroundColor = consoleColor;
                    }
                    if (j 1 || j >= 8)
                    {
                        Console.ForegroundColor = consoleColor;
                    }
                    break;
                case 9:
                    if (i 1 || i == 9 || i == 10 || i >= 18)
                    {
                        Console.ForegroundColor = consoleColor;
                    }
                    if (i 10 && j 1)
                    {
                        Console.ForegroundColor = consoleColor;
                    }
                    if (j >= 8)
                    {
                        Console.ForegroundColor = consoleColor;
                    }
                    break;
            }
        }
    }
}
C# Code

注:以上代码属个人整理,用于交流学习,原创。转载请标明出处。(QQ/微信:742010299 昵称:同心同德)

asp.net C# 控制台模拟红绿灯(交通信号灯)

标签:不能   执行   inpu   yellow   while   name   OLE   returns   readline   

原文地址:https://www.cnblogs.com/tongxintongde/p/11768210.html


评论


亲,登录后才可以留言!