Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将整数输入保存到数组中_C#_Arrays_String_Console_Integer - Fatal编程技术网

C# 将整数输入保存到数组中

C# 将整数输入保存到数组中,c#,arrays,string,console,integer,C#,Arrays,String,Console,Integer,我试图将整数输入保存到数组中,但它不起作用。我从中找到了一个字符串保持的示例 string[]yazi=新字符串[15]; for(int i=0;i

我试图将整数输入保存到数组中,但它不起作用。我从中找到了一个字符串保持的示例

string[]yazi=新字符串[15];
for(int i=0;i
但当我把这个代码转换成整数时,它给出了一个错误

int[] sayis = new int[20];
for (int k = 0; k < sayis.Length; k++)
{
      sayis[k] = int.Parse(Console.ReadLine());
}
int[]sayis=newint[20];
for(int k=0;k
我错过什么了吗

我错过什么了吗

一方面,错误消息

只要在控制台中键入整数,就可以了。(我刚刚试过,效果很好。)如果用户输入的值不能解析为整数,您将得到
FormatException
。你应该考虑使用…这将在
out
参数中设置值,并返回是否实际成功。例如:

for (int k = 0; k < sayis.Length; k++)
{
    string line = Console.ReadLine();
    if (!int.TryParse(line, out sayis[k]))
    {
        Console.WriteLine("Couldn't parse {0} - please enter integers", line);
        k--; // Go round again for this index
    }
}
for(int k=0;k
我错过什么了吗

一方面,错误消息

只要在控制台中键入整数,就可以了。(我刚刚试过,效果很好。)如果用户输入的值不能解析为整数,您将得到
FormatException
。你应该考虑使用…这将在
out
参数中设置值,并返回是否实际成功。例如:

for (int k = 0; k < sayis.Length; k++)
{
    string line = Console.ReadLine();
    if (!int.TryParse(line, out sayis[k]))
    {
        Console.WriteLine("Couldn't parse {0} - please enter integers", line);
        k--; // Go round again for this index
    }
}
for(int k=0;k
只要我输入整数就可以了。您得到了什么错误?输入字符串的格式不正确。只要输入整数,就可以正常工作。您得到了什么错误?输入字符串的格式不正确,但我得到了错误,我没有向控制台提供任何输入。我的意思是,当控制台运行时,我会出错。我试试int。TryParse@Merve:对。如果你不输入一个数字(包括如果你没有输入任何东西就按return键),就会发生这种情况。但是我得到了一个错误,我没有向控制台提供任何输入。我的意思是,当控制台运行时,我会出错。我试试int。TryParse@Merve:对。如果您不输入数字(包括在不输入任何内容的情况下按return键),就会发生这种情况。