简体   繁体   English

如何创建一个二维数组 [n,3] 从用户那里获取字符串输入 c# c sharp

[英]how to create a 2d array [n,3] getting string input from the user in c# c sharp

I wrote this code that sum the elements vertically and horizontally.我写了这段代码,垂直和水平地对元素求和。 My problem is creating the 2d array from string input.我的问题是从字符串输入创建二维数组。 The user should input a string like "2 1 6" on a single line (separated by space) and I must to convert into integer each number.用户应该在一行中输入类似"2 1 6"的字符串(以空格分隔),我必须将每个数字转换为 integer。 The first input of the code represent the number of next lines with strings like "2 1 6" .代码的第一个输入表示下一行字符串的数量,如"2 1 6" For example, input:例如,输入:

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

The array should be: int [,] arr = {{2,1,6},{3,1,8}} My code:该数组应该是: int [,] arr = {{2,1,6},{3,1,8}}我的代码:

using System;

namespace Exercises
{
    class Program
    {
        static void Main()
        {
            string input1 = Console.ReadLine();
            int n = Convert.ToInt32(input1);
            
            string input2 = Console.ReadLine();
            string[,] strArr = new string[n, 3];
  
            int rows, cols;                   

            for (int i = 0; i < n; i++)
            {

                for (int j = 0; j < 3; j++)
                {
                    strArr[i, j] = input2.Split(' ')[i];                    
                }
            }            
            rows = strArr.GetLength(0);
            cols = strArr.GetLength(1);

            int[,] intArr = new int[strArr.GetLength(0), strArr.GetLength(1)];
            
            for (int i = 0; i < strArr.GetLength(0); i++)
            {
                for (int j = 0; j < strArr.GetLength(1); i++)
                {
                    intArr[i, j] = Convert.ToInt32(strArr[i, j].ToString());
                }
            }

            for (int i = 0; i < rows; i++)
                {
                    int sumRow = 0;
                    for (int j = 0; j < cols; j++)
                    {
                        sumRow += intArr[i, j];
                    }
                    Console.WriteLine("Livada " + (i + 1) + ": " + sumRow);
                }
            string[] treesName = { "Meri", "Peri", "Ciresi" };
            for (int i = 0; i < cols; i++)
             {
                    int sumCol = 0;
                    for (int j = 0; j < rows; j++)
                    {
                        sumCol += intArr[j, i];

                    }
                    Console.WriteLine($"{treesName[i]}: " + sumCol);
                }


            }

        }
    }

Is anybody here who can help me with that?这里有人可以帮助我吗? Thank you!谢谢!

This program reads the numbers from the Console and fills in the array.该程序从Console读取数字并填充数组。 The key here is using the .Split() method to split a line input to an array of string, separated by the spaces.这里的关键是使用.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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM