2015-11-24 37 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.Write线FormatException

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]);