namespace test
{
class Test6
{
static void Main(string[] args)
{
string str = "welcome to xfht";
//获取字符串str的长度
int length = str.Length;
//获取控制台窗口的宽度
int width=Console.WindowWidth;
//在字符串前添加width-length个空格
for (int i = 1; i <= width - length; i++)
{
str = " " + str;
}
int index = 0;
while (true)
{
Console.Clear();//清除控制台中显示的信息(清屏)
//截取字符串
string temp = str.Substring(index);
Console.Write(temp);
index++;
System.Threading.Thread.Sleep(100);
if (index > width)
{
index = 0;
}
}
}
}
}
C#实现跑马灯效果
最新推荐文章于 2023-02-11 00:41:44 发布
本文介绍了如何使用C#编程语言,在控制台窗口中通过循环和字符串操作实现字符串的动态打印,并在每次打印后清空控制台窗口。详细解释了获取字符串长度、控制台窗口宽度、添加空格以及使用Substring和Console.Clear方法的用法。
880

被折叠的 条评论
为什么被折叠?



