iPhone数学不太符合逻辑
我在添加数组中的值,但其和实际上应该是+1iPhone数学不太符合逻辑,iphone,objective-c,arrays,math,integer,Iphone,Objective C,Arrays,Math,Integer,我在添加数组中的值,但其和实际上应该是+1 //update totalscore uint newTotalScore; for (uint i=0; i< [bestscoresArray count] ; i++) { newTotalScore += [[bestscoresArray objectAtIndex:i] intValue]; } totalscore = newTotalScore; //更新
//update totalscore
uint newTotalScore;
for (uint i=0; i< [bestscoresArray count] ; i++) {
newTotalScore += [[bestscoresArray objectAtIndex:i] intValue];
}
totalscore = newTotalScore;
//更新totalscore
uint新核心;
对于(uint i=0;i<[bestscoresArray count];i++{
newTotalScore+=[[bestscoresArray对象索引:i]intValue];
}
总分=新总分;
//输出
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;
否则它将是未定义的。在您的情况下,它是
1
,但它可能是任何其他值。对此不确定,但您是否尝试将newTotalScore
初始化为零?(请参阅关于变量初始化。)如果这没有帮助,请提供更多代码