首页 > 解决方案 > 如何在 C# 数组中的字符串的特定索引处打印一个字母?

问题描述

我一直在尝试做一个效果,其中控制台一次输入一个字符串数组元素的每个字母,中间有一点延迟。我一生都无法弄清楚如何在数组中字符串的特定索引处打印一个字母(或者如果它甚至可能的话)。

我一直在尝试通过使用 for 循环来做到这一点,每次写入完整元素时将索引增加 1,然后进入下一个。问题是如何单独打印每个字母。现在,它只打印整个字符串元素,然后转到下一个,依此类推。我希望它单独打印每个字母,然后增加元素索引。

我已经研究了几个小时,但似乎找不到任何东西。

public string[] menuInputs = { };

    public void TextAnimateScrollLeft()
    {
        menuInputs = File.ReadAllLines("../MenuInputs.txt");
        menuInputs.Reverse();

        int arrayIndex;
        for(arrayIndex = 0; arrayIndex <= menuInputs.Length - 1; arrayIndex++)
        {
            Console.Write(menuInputs[arrayIndex]);
            Thread.Sleep(50);
        }

        Console.ReadKey();
    }

标签: c#console-application

解决方案


注释包含在注释中:

//it's not clear just from the ScrollLeft name: 
// do you also want to scroll each string backwards?
// If so, you'll need to make use of the `SetPosition()` function
public void TextAnimateScrollLeft()
{
    var menuInputs = File.ReadAllLines("../MenuInputs.txt");

    //save the work reversing the array by iterating backwards
    for(int arrayIndex = menuInputs.Length -1; arrayIndex >= 0; arrayIndex--)
    {
       // also loop through the characters in each string
       foreach(char c in menuInputs[arrayIndex])
       {
           Console.Write(c);
           Thread.Sleep(50); // 20 characters per second is still pretty fast
       }
       Console.WriteLine(); //don't forget the line break
    } 

    Console.ReadKey();
}

推荐阅读