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

関連する問題