2016-11-28 59 views
-4

控制台提示用户输入他的数组,我可以在用户输入他的输入之前放置一个0的列,我需要的是将用户完成后数组末尾的一串0。如何在数组末尾添加一列0 C#

谢谢

+1

对于那些谁是关闭的范围太广。这里是他所使用的完整代码HTTP:/ /stackoverflow.com/a/40761005/2592042 –

+0

谢谢你提到这一点。 – Johnny

回答

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()); 
     } 
    } 
    } 
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; 
    } 
+0

谢谢你的帮助。 – Johnny

相关问题