这是我试过的代码
int[] WeeklyTotal = new int[53];
for (int w = 1; w <= 53; w++)
{
WeeklyTotal[w] = WeeklyTotal[w] + data.Rows[i]["week" + w]; // Error is here
}
但是我遇到了一个编译错误:
Can not apply opperator + to opperands of type int and object
这里正确的语法是什么?
谢谢!
最佳答案
您需要将 object
类型转换为 integer
类型
WeeklyTotal[w] = WeeklyTotal[w] + (int)data.Rows[i]["week" + w];
还需要更新循环逻辑的数组迭代,否则索引将超出范围,并且会抛出 IndexOutOfRangeException
for (int w = 0; w < WeeklyTotal.Length; w++)
{
WeeklyTotal[w] = WeeklyTotal[w] + (int)data.Rows[i]["week" + w];
}
关于c# - 将一个 int 添加到它的现有数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38771371/