C#實現跑馬燈效果 .

 
  1. namespace test  
  2. {  
  3.     class Test6  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             string str = "welcome to xfht";  
  8.             //獲取字符串str的長度   
  9.             int length = str.Length;  
  10.             //獲取控制檯窗口的寬度   
  11.             int width=Console.WindowWidth;  
  12.             //在字符串前添加width-length個空格   
  13.             for (int i = 1; i <= width - length; i++)  
  14.             {  
  15.                 str = " " + str;  
  16.             }  
  17.   
  18.             int index = 0;  
  19.             while (true)  
  20.             {  
  21.                 Console.Clear();//清除控制檯中顯示的信息(清屏)   
  22.                 //截取字符串   
  23.                 string temp = str.Substring(index);  
  24.                 Console.Write(temp);  
  25.                 index++;  
  26.                 System.Threading.Thread.Sleep(100);  
  27.                 if (index > width)  
  28.                 {  
  29.                     index = 0;  
  30.                 }  
  31.             }  
  32.         }  
  33.     }  
  34. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章