c#如何使加载文本变得简单

时间:2016-09-26 04:23:18

标签: c#

这就是我到目前为止所做的一切,一切都会好起来,直到它成为数组中的最后一个字符串

private static String[] loading = {"Loading.", "Loading..", "Loading..."};

public static void Main(String[] args)
{
    while (true)
    {
        for (int i = 0; loading.Length > i; i++)
        {
            Console.Write("\r" + loading[i]);
            Thread.Sleep(500);
        }
    }

2 个答案:

答案 0 :(得分:3)

代码稍微简单一些。可能有用。使用mod而不是重置

 int i = 0;
 while (true)
 {
     i = i % 3;
     Console.Write("\r" + loading[i++]);
     Thread.Sleep(500);               
 }

答案 1 :(得分:0)

如果你用一行写它会更好看:

public static void Main()
{
    int loadingLength = 5;
    Console.Write("Loading...");

    for (int i = 0; i < loadingLength; i++)
        Console.Write(".");
        Thread.Sleep(500);
    }
}

如果您仍然需要输出多行“加载”,请考虑生成长度为N的点串,而不是将它们全部存储在数组中。
提示:new string(char, count)使用字符count生成长度为char的字符串。