天天看點

C#控制台輸入字元串跑馬燈效果

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace no3_sj5
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("請輸入字元串");
            string str = Console.ReadLine();
            string tem = str;
            bool flag = true;
            while (true)
            {
                Console.Clear();//清空緩存
                Console.WriteLine(tem);
                System.Threading.Thread.Sleep(100);//指定線程時間
                if (flag)
                {

                    if (tem.Length < Console.WindowWidth)
                    {
                        tem = tem.Insert(0, " ");
                    }
                    else
                    {
                        flag = false;
                    }
                }
                else
                {
                    if (tem.Length > str.Length)
                    {
                        tem = tem.Remove(0, 1);
                    }
                    else
                    {
                        flag = true;
                    }
                }

            }
        
        }
      
    }
}