cro*_*y88 0 arrays iphone math integer objective-c
我正在添加数组中保存的值,但总和是实际应该是+1.
//update totalscore
uint newTotalScore;
for (uint i=0; i< [bestscoresArray count] ; i++) {
newTotalScore += [[bestscoresArray objectAtIndex:i] intValue];
}
totalscore = newTotalScore;
Run Code Online (Sandbox Code Playgroud)
//输出l1bestscore = 15900,l2bestscore = 7800,l3bestscore = 81000,l4bestscore = 81000,l5bestscore = 0,l6bestscore = 0,l7bestscore = 0,l8bestscore = 0,l9bestscore = 0,l10bestscore = 0,totalscore = 185701
如您所见,totalscore输出为185701,但所有值的总和为185700.
有人会有任何想法为什么会这样吗?
谢谢,
标记
您必须定义newTotalScore初始值:
uint newTotalScore = 0;
Run Code Online (Sandbox Code Playgroud)
否则它将是未定义的.在你的情况下,1它可能是任何其他价值.
| 归档时间: |
|
| 查看次数: |
106 次 |
| 最近记录: |