如何使用二维数组

时间:2019-10-21 07:36:13

标签: c#

所以我有以下示例:

我有这3个字符串:

string seq3 = "Zeile1: 5,4,2; Zeile2: 9,4,8; Zeile3: 5,3,6"; 
string seq4 = "Zeile1: 2,5,4,2; Zeile2: 4,1,7,8; Zeile3: 5,3,6,1; Zeile4: 9,2,3,5"; 
string seq5 = "Zeile1: 2,7,5,4,2; Zeile2: 9,4,1,7,8; Zeile3: 5,3,6,7,1; Zeile4: 9,2,3,5,0; Zeile5: 7,2,5,1,6";

所以我需要将字符串分割成5 4 2等,或者2 5 4 2 我应该像矩阵一样输出它。 例如:

5 4 2
9 4 8
5 3 6

最后,我需要计算算术中间值。 a + b / 2

所以这是我的分割方法,但是尺寸不起作用。我认为方法签名是错误的,我应该使用两个昏暗的数组。

private static string[,] myArray = new string[5, 5]; 

        public string Berechnen(string s)
        {
            string result ="";

            string[] ZeilenInhalt = s.Split(';');
            for (int i = 0; i < 5; i++)
            {
                string[] daten = ZeilenInhalt[i].Split(':');
                string[] ascciizahlen = daten[1].Split(',');
                for (int j = 0; j < 5; j++)
                {
                    result = myArray[i, j] = ascciizahlen[j];
                }
            }
            return result;
        }

这是输出方法

   public void Show()
        {
            for (int i = 0; i < myArray.GetLength(0); i++)
            {
                for (int j = 0; j < myArray.GetLength(1); j++)
                {
                    Console.Write($"{myArray[i, j]}");
                    int middle = Convert.ToInt32(myArray[i, j]);
                    Console.WriteLine($"{middle,1}");
                }

            }
        }

我使用了插值字符串,因此可以将其四舍五入为逗号。

输出应该像这样:

5 4 2
9 4 8
5 3 6

Middle value: 123,2 

我只有黑屏。

谢谢

2 个答案:

答案 0 :(得分:0)

如果我对您的理解正确,也许您可​​以做这样的事情?

        string seq3 = "Zeile1: 5,4,2; Zeile2: 9,4,8; Zeile3: 5,3,6";
        string seq4 = "Zeile1: 2,5,4,2; Zeile2: 4,1,7,8; Zeile3: 5,3,6,1; Zeile4: 9,2,3,5";
        string seq5 = "Zeile1: 2,7,5,4,2; Zeile2: 9,4,1,7,8; Zeile3: 5,3,6,7,1; Zeile4: 9,2,3,5,0; Zeile5: 7,2,5,1,6";

        var resultArray = seq5.Split(';').Select(s => s.Split(':')[1].Trim().Split(',').Select(n => int.Parse(n)).ToArray()).ToArray();

        foreach (var subArray in resultArray)
        {
            foreach (var number in subArray)
            {
                Console.Write(number);
            }

            Console.WriteLine($" (line average: {subArray.Average()})");
        }

        Console.ReadKey();

是您需要的每个子数组行的中间值吗?

答案 1 :(得分:-1)

请尝试以下拆分方法:

public string[] GetMatrixFromString(string seq)
{           
     seq = seq.Replace("Zeile", "");
     var array = seq.Split(';');

     if(array.Length > 0)
     {
          string[] matrix = new string[array.Length];

          for (int i = 0; i < array.Length; i++)
          {
               var item = array[i];
               var matrixLine = item.Split(':')[1].Replace(",", " ");
               matrix[i] = matrixLine;
          }

          return matrix;
      }
      else
      {
          return null;
      }          
}

请小心检查返回值是否为null,以免在这种情况下它无法解析字符串并创建矩阵。

例如:

string seq3 = "Zeile1: 2,5,4,2; Zeile2: 4,1,7,8; Zeile3: 5,3,6,1; Zeile4: 9,2,3,5";
var matrix = GetMatrixFromString(seq3);

if(matrix == null)
{
      Console.WriteLine("Unable to parse input");
}
else
{
      foreach (var line in matrix)
      {
            Console.WriteLine(line);
      }
}