如何在数组C#末尾添加0列

时间:2016-11-28 12:05:59

标签: c# arrays loops for-loop

控制台提示用户输入他的数组,我可以在用户输入输入之前输入0的col,我需要的是在用户完成后在数组的末尾添加0的col

谢谢

2 个答案:

答案 0 :(得分:1)

for (int row = 0; row < r; row++)
    {
        for (int col = 0; col < c-1; col++)
        {

            Console.Write("Enter value for matrix[{0},{1}] = ", row, col - 1);
            matrix[row, col] = (int)Convert.ToInt32(Console.ReadLine());
        }
        matrix[row, col] = 0;
    }

答案 1 :(得分:1)

我确信您使用此代码在矩阵之前插入0。 How to add a column to the an array C#

此代码所需的更改是

//c++; //remove this line
c = c + 2; //add two extra column for adding 0's, One for beginning one for end
int[,] matrix = new int[r, c];   

其他更改

    for (int row = 0; row < r; row++)
    {
      for (int col = 0; col < c - 1; col++) // reduce column loop by one
      {
        if (col == 0)
        {
          matrix[row, col] = 0;
        }
        else
        {
          Console.Write("Enter value for matrix[{0},{1}] = ", row, col - 1);
          matrix[row, col] = (int)Convert.ToInt32(Console.ReadLine());
        }
     }
   }