-4 c#
我正在添加两个1-D数组,但我在编写的代码中出错:错误:代码中有未处理的异常.
static void Main()
{
// setup two test arrays
int[] x = new int[] { 2, 4, 6 };
int[] y = new int[] { 3, 6, 9 };
// invoke method and store result
int[] z = AddVectors(x, y);
}
static int[] AddVectors(int[] a, int[] b)
{
// check that both arrays are of the same length
if(a.Length == b.Length ) return null;
// create a new array to store result
int[] c = new int[a.Length];
// carry out addition term by term
for (int i = 0; i <= c.Length; i++)
{
c[i] = a[i] + b[i];
}
// return resulting array
return c;
}
}
Run Code Online (Sandbox Code Playgroud)
你应该检查:
if(a.Length != b.Length ) return null;
Run Code Online (Sandbox Code Playgroud)
PS:尝试更简明扼要地说明你的问题是什么.