c# - 如何在 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();
}
解决方案
注释包含在注释中:
//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();
}
推荐阅读
- python - OpenCV 中 L*a*b* 颜色空间范围的负值
- flutter - 使用 Flutter 创建适用于 iOS 或 Android 的新应用
- powershell - 空参数的 Powershell 错误处理
- android - 反应原生 Flatlist renderItem 不更新
- reactjs - 安全的本地主机无法访问网站 (food2fork) api
- c++ - 循环内奇怪的内存泄漏
- request - 我怎么称呼未来
在颤动中休息api? - poppler - pdf2HtmlEX - html 上的文本与源 pdf 不同
- tizen - 本机 Tizen 3.0 表盘严重错误。对待:2
- python - /post/106/ 处的 AttributeError