如何在 C# 中的数组中的字符串的特定索引处打印字母?
How to print a letter at a specific index of a string that is in an array in 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();
}
我一直在尝试实现一种效果,即控制台一次一个地键入字符串数组元素的每个字母,中间稍有延迟。我一辈子都想不出如何在数组中字符串的特定索引处打印一个字母(或者如果可能的话)。
我一直在尝试通过使用 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();
}