如何在c#c sharp中创建一个2d数组[n,3],从用户处获取字符串输入

如何在c#c sharp中创建一个2d数组[n,3],从用户处获取字符串输入,c#,arrays,string,matrix,input,C#,Arrays,String,Matrix,Input,我编写了这段代码,对元素进行垂直和水平求和。我的问题是从字符串输入创建二维数组。用户应该在单行上输入一个字符串,如“2 1 6”,我必须将每个数字转换为整数。代码的第一个输入表示带有字符串的下一行数,如“2 1 6”。例如,输入: 2 // the first input line 2 1 6 // the 2nd input line with the string 3 1 8 // the 3nd input line with the string

我编写了这段代码,对元素进行垂直和水平求和。我的问题是从字符串输入创建二维数组。用户应该在单行上输入一个字符串,如
“2 1 6”
,我必须将每个数字转换为整数。代码的第一个输入表示带有字符串的下一行数,如
“2 1 6”
。例如,输入:

2           // the first input line 
2 1 6      // the 2nd input line with the string
3 1 8     //  the 3nd input line with the string
数组应该是:
int[,]arr={{2,1,6},{3,1,8}
我的代码:

使用系统;
名称空间练习
{
班级计划
{
静态void Main()
{
字符串input1=Console.ReadLine();
int n=转换为32(输入1);
字符串input2=Console.ReadLine();
字符串[,]strArr=新字符串[n,3];
int行,cols;
对于(int i=0;i

有人能帮我吗?谢谢大家!

此程序从
控制台读取数字并填充数组。这里的关键是使用
.Split()
方法将行输入拆分为字符串数组,并用空格分隔

class Program
{
    static void Main(string[] args)
    {
        var input = Console.ReadLine();
        var n = int.Parse(input);

        int[,] intArr = new int[n, 3];

        for (int i = 0; i < n; i++)
        {
            input = Console.ReadLine();
            var parts = input.Split(' ');

            for (int j = 0; j < 3; j++)
            {
                intArr[i, j] = int.Parse(parts[j]);
            }                
        }
        // intArray has the values from the input
    }
}
类程序
{
静态void Main(字符串[]参数)
{
var input=Console.ReadLine();
var n=int.Parse(输入);
int[,]intArr=新的int[n,3];
对于(int i=0;i
这是[C#]还是什么语言?请添加相关标签。对不起,我更新了问题和标签。这里是我的第一个问题……谢谢。第一行的输入是什么?每行必须具有相同的列数?问题是非常非常不明确的。如果关于对角线的问题以可回答的格式返回,那么代码的第一行用字符串表示下一行的数量,如“2 1 6”。请用@myname打电话给我。为什么你假设每行只有3个数字?因为这是一个练习,每行只有3个元素。我遵循op的假设。这肯定是一个
Dcoder
挑战问题,输入是定义的,不是任意的。这个答案对我帮助很大,帮助我解决我的练习题。非常感谢。嘿,@johnalexou!我现在给你投票权了。我希望现在一切都好:)
class Program
{
    static void Main(string[] args)
    {
        var input = Console.ReadLine();
        var n = int.Parse(input);

        int[,] intArr = new int[n, 3];

        for (int i = 0; i < n; i++)
        {
            input = Console.ReadLine();
            var parts = input.Split(' ');

            for (int j = 0; j < 3; j++)
            {
                intArr[i, j] = int.Parse(parts[j]);
            }                
        }
        // intArray has the values from the input
    }
}