2015-11-24 70 views
-5

我的編碼有什麼問題...?陣列附加異常錯誤:

這是我添加二維數組的編碼。當我調試我的編碼,然後未處理的異常system.format.something的時...

代碼..

static void Main(string[] args) 
{ 
    int[,] a = new int[4, 4] 
    { { 1, 1, 1, 1 }, 
     { 1, 1, 1, 1 }, 
     { 1, 1, 1, 1 }, 
     { 1, 1, 1, 1 } }; 


    int[,] b = new int[4, 4] 
    { { 2, 2, 2, 2 }, 
     { 2, 2, 2, 2 }, 
     { 2, 2, 2, 2 }, 
     { 2, 2, 2, 2 } }; 

    int[,] c = new int[4, 4]; 

    for (int row = 0; row < 4; row++) 
    { 
     for (int col = 0; col < 4; col++) 
     { 
      c[row, col] = a[row, col] + b[row, col];     
     } 
    } 

    for (int row = 0; row < 4; row++) 
    { 

     for (int col = 0; col < 4; col++) 
     { 
      Console.Write("The value of {0},{1}", c[row, col]);     
     } 
    } 
} 
+0

究竟是什麼異常?在哪一行?並根據您的具體問題[寫一個更好的標題](http://meta.stackexchange.com/q/10647/158761)。 –

回答

3

您使用Console.Write並指定兩個參數,但你只有通過一個

此:

Console.Write("The value of {0},{1}", c[row, col]); 

應該是沿着線:

Console.Write("The value of row: {0}, column: {1} is {2}", row, col, c[row, col]); 
3

運行在LINQPad的代碼,我得到了Console.WriteFormatException

FormatException
Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

這是因爲您的格式字符串需要兩個參數,但您只能傳遞一個參數。嘗試將該行更改爲

Console.WriteLine("The value of {0},{1} is {2}", row, col, c[row,col]);