首页 文章

NSInteger不能在相同的乘法中使用两次[关闭]

提问于
浏览
-3

我很难过!我正在使用NSinteger,但由于我的所有输入都是整数,因此结果是整数我无法看到由于四舍五入而进行除法的人所面临的问题 .

我查看了NSInteger的开发者指南,但找不到警告 . 我搜索这个网站和谷歌没有产生结果 .

正如我在最后陈述的那样,我可以解决它,但我觉得有一些简单的我想念 .

所有变量都是直接的,在第一个for循环中没有指针,intergerOfInterest是0,1,6,7,4,5,10,1这是非常随机的,我试过交换integerValueA ^ 2进行计算但是没有影响 . 然而,第二个for循环给出正确的答案,6,12,20,30,2,56,72,90

for (loopCount = 0; loopCount < 8; loopCount++){
        integerValueA = loopCount + 2;
        integerValueB = integerValueA + 1;
        intergerOfInterest = (integerValueA) * (integerValueA);
    }


    for (loopCount = 0; loopCount < 8; loopCount++){
        integerValueA = loopCount + 2;
        integerValueB = integerValueA + 1;
        intergerOfInterest = (integerValueA) * (integerValueB);
    }

结构中有几个循环,正确数字和错误数字之间的公因子是在计算中多次使用NSInteger . 我认为这不可能是正确的,所以有人知道我是怎么弄错的 . 注意:如果我添加一个额外的变量来存储数字的第二个实例(所以在第一个循环中我使用“(integerValueA)*(integerValueB - 1)”它工作正常 .

注意:编辑使用命名约定 .

2 回答

  • 0

    从评论中的讨论中可以看出,您的原始代码看起来像这样:

    for(int i = 0; i < 8; i++) {
        nVal = i ^ 2; // Supposed to be equivalent to nVal = i * i;
        // Do something with nVal
    }
    

    C中的 ^ 运算符实际上是按位的XOR运算符,而不是指数运算符 . 上面的代码需要 i ,翻转位1,并将结果赋给 nVal .

    您想要使用以下任一项:

    // Option 1
    for(int i = 0; i < 8; i++) {
        nVal = i * i;
        // Do something with nVal
    }
    
    // Option 2
    for(int i = 0; i < 8; i++) {
        nVal = pow(i, 2);
        // Do something with nVal
    }
    
  • 0

    你的变量InteRgerOfInterest而不是IntEgerOfInterest有可能出现拼写错误吗?

    GH

相关问题