Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.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# 获取字符串输入并生成int数组_C# - Fatal编程技术网

C# 获取字符串输入并生成int数组

C# 获取字符串输入并生成int数组,c#,C#,如何获取字符串输入并使其成为int数组 string input = Console.ReadLine(); int numb = Convert.Toint32(input); int[] intArray = // what do i write here to make it take the "input" length, and put the input into an int array? 您没有给出太多细节,但如果输入是逗号分隔的数字列表,则可以执行以下操作

如何获取字符串输入并使其成为int数组

string input = Console.ReadLine();
int numb = Convert.Toint32(input);          
int[] intArray = // what do i write here to make it take the "input" length, and put the input into an int array?

您没有给出太多细节,但如果输入是逗号分隔的数字列表,则可以执行以下操作:

string input = "1,2, 3,4  ,5 ,6";  // string to simulate input
int[] numbers = input.Split(new char[] {','})
                     .Select(s => int.Parse(s))
                     .ToArray();

如果逗号之间的任何字符串都不是有效的整数,这显然会爆炸。

可以通过两种不同的方式从字符串获取数组

您可以使用split方法,该方法将字符串拆分为一个子字符串数组,然后必须解析该数组的每个元素。这可能是您想要的,因为您似乎想要一个整数数组

或者可以将字符串转换为字节数组。然后将这些值转换为您认为合适的整数。

string input=Console.ReadLine();
string input = Console.ReadLine();
int numb = Convert.ToInt32(input);
int[] intArray = new int[numb];
for (int i; i < intArray.length; i++)
{
   intArray[i] = numb;
}
int numb=Convert.ToInt32(输入); int[]intArray=新int[numb]; for(int i;i
要获取输入的长度,可以执行以下操作:

    string input = Console.ReadLine();
    int numb = input.Length;
    int[] intArray = new int[1];
    intArray[0] = numb;

您能给出输入字符串的示例以及该字符串的输出应该是什么吗?可能的