【问题标题】:How to add a column of 0s at the end of an array C# [closed]如何在数组 C# 的末尾添加一列 0 [关闭]
【发布时间】:2016-11-28 12:05:58
【问题描述】:

控制台提示用户输入他的数组,我能够在用户输入他的输入之前放置一个 0 的列,我需要在用户完成后在数组的末尾放置一个 0 的列.

谢谢

【问题讨论】:

标签: c# arrays loops for-loop


【解决方案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());
        }
     }
   }

【讨论】:

    【解决方案2】:
    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;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      • 2010-12-22
      • 1970-01-01
      • 2021-04-24
      • 2012-03-27
      • 2018-12-22
      • 2012-11-02
      相关资源
      最近更新 更多