【发布时间】:2015-01-10 02:24:17
【问题描述】:
使用我的数字返回因子函数,我得到了一个我不应该出现的错误。
这是我的代码:
static void Main(string[] args)
{
foreach (int element in Factors(16))
{
Console.Write(element.ToString() + ", ");
}
}
static Array Factors(double value)
{
int[] factors = new int[] { };
int counter = 0;
for (int i = 1; i <= value; i++)
{
if (value % i == 0)
{
factors[counter] = i;
counter++;
}
}
return factors;
}
这是我得到的错误:
TestSolver.exe 中出现“System.IndexOutOfRangeException”类型的未处理异常
附加信息:索引超出了数组的范围。
【问题讨论】:
-
你没有为因子分配任何空间.....
-
factors是一个空数组。里面有什么可以索引的?
标签: c# indexing indexoutofboundsexception