Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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
用户输入的整数数组(Beginer C#)_C#_Arrays - Fatal编程技术网

用户输入的整数数组(Beginer C#)

用户输入的整数数组(Beginer C#),c#,arrays,C#,Arrays,我是C#的新手。我有一个关于在数组中填充它的问题。在我必须编写的程序中,我得到了“来自用户的输入:由空格分隔的不同数量的数字(例如:1 4 6 2)”。这些数字必须输入到以后使用的数组中。 问题在于如何在数组中插入一行上写有空格的无限数量的数字,而不事先确定它们的数字?我认为更简单的方法是按空格分割输入字符串,然后使用Linq将每个元素映射到int: String userInput = "1 4 6 2"; int[] ints = userInput.Split(' ')

我是C#的新手。我有一个关于在数组中填充它的问题。在我必须编写的程序中,我得到了“来自用户的输入:由空格分隔的不同数量的数字(例如:1 4 6 2)”。这些数字必须输入到以后使用的数组中。
问题在于如何在数组中插入一行上写有空格的无限数量的数字,而不事先确定它们的数字?

我认为更简单的方法是按空格分割输入字符串,然后使用Linq将每个元素映射到int:

String userInput = "1 4 6 2";
int[] ints = userInput.Split(' ').Select(x => Int32.Parse(x)).ToArray();

这就是我问题的答案。我希望我曾经帮助过别人

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Stack_Sum
 {
class Program
  {
    static void Main(string[] args)
    {
        Stack<int> numbers = new Stack<int>();
        string input = Console.ReadLine();
        string[] number = Regex.Split(input, @"\D+");
        foreach (string value in number)
        {
            if (!string.IsNullOrEmpty(value))
            {
                int i = int.Parse(value);
                Console.WriteLine("Number: {0}", i);
            }
        }

    }

  }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text.RegularExpressions;
名称空间堆栈和
{
班级计划
{
静态void Main(字符串[]参数)
{
堆栈编号=新堆栈();
字符串输入=Console.ReadLine();
string[]number=Regex.Split(输入@“\D+”);
foreach(以数字表示的字符串值)
{
如果(!string.IsNullOrEmpty(值))
{
int i=int.Parse(值);
WriteLine(“编号:{0}”,i);
}
}
}
}
}

假设您的输入是来自用户的字符串,请使用string.split()返回数组。此处的文档:。之后是返回字符串数组还是转换为整数数组取决于您。我理解,非常感谢。这能回答您的问题吗?