所以我试图用用户输入填充一个字符数组。但是我不想自己指定数组的长度,从而将用户限制在他们可以输入的数量。例如,我下面的代码将只允许用户输入 5 个字符然后退出。任何语言的代码都可以。(下面是C#)。
Console.Write("Enter a number of characters of your choice...click 1 to exit: ");
bool exitCondition;
int counter = 0;
char[] character = new char[5];
do
{
exitCondition = false;
try
{
character[counter] = char.Parse(Console.ReadLine());
if (character[counter] == '1')
exitCondition = true;
}
catch (Exception ex)
{
Console.WriteLine("Error: {0}", ex.Message);
}
counter++;
}
while (exitCondition == false && counter < 5);