2016-11-28 58 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

相關問題