【问题标题】:iPhone maths not quite adding up rightiPhone数学加起来不太对
【发布时间】:2011-05-12 15:56:29
【问题描述】:

我正在添加一个数组中保存的值,但总和是 +1 实际应该是什么。

//update totalscore
    uint newTotalScore;

    for (uint i=0; i< [bestscoresArray count] ; i++) {      
        newTotalScore += [[bestscoresArray objectAtIndex:i] intValue];  

    }


    totalscore = newTotalScore;

//输出 l1bestscore=15900, l2bestscore=7800, l3bestscore=81000, l4bestscore=81000, l5bestscore=0, l6bestscore=0, l7bestscore=0, l8bestscore=0, l9bestscore=0, l10bestscore=0, totalscore=185701

如您所见,总分输出为 185701,但所有值的总和为 185700。

有人知道为什么会发生这种情况吗?

谢谢,

标记

【问题讨论】:

    标签: iphone objective-c arrays math integer


    【解决方案1】:

    你必须定义newTotalScore的初始值:

    uint newTotalScore = 0;
    

    否则它将是未定义的。在您的情况下,它是 1,但它可能是任何其他值。

    【讨论】:

      【解决方案2】:

      对此不确定,但您是否尝试将newTotalScore 初始化为零? (有关变量初始化,请参阅 this question。)如果这没有帮助,请给我们更多代码。

      【讨论】:

        猜你喜欢
        • 2022-01-14
        • 1970-01-01
        • 1970-01-01
        • 2020-02-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-10
        相关资源
        最近更新 更多