我认为陷入循环,不能使用循环外设置的变量



好的,所以我的程序似乎运行良好并完成了我需要的所有计算,程序需要的最后一部分是递增的剪切列表。因此,我尝试构建一个 NSMutable 数组并使用先前设置的变量,我最终将用这些变量填充一个表。只有当我运行代码并点击计算按钮时,程序才会冻结,并且 NSLOG 等中没有任何显示。我可以不使用以前在 for 循环之外设置的变量吗?还是怎么回事?

-(IBAction)calculateGo
{
    BuildNavAppDelegate *buildNavDelegate = (BuildNavAppDelegate *)[[UIApplication sharedApplication] delegate];
    float iridge = ([ridge.text intValue]);
    float ihip = ([hip.text intValue]);
    float ispacing = ([rafterSpace.text intValue]);
    float floatTLPMR = [buildNavDelegate.TLPMR floatValue];
    int floatRafter = [buildNavDelegate.raftThicknessPassed intValue];
    int comraftbird = [buildNavDelegate.comRaftBirdPassed intValue];
    float mitre = (45 * M_PI) / 180;
    float y = tanf(mitre);
    float mitreThickness = sqrt((y*y)+1) * ihip;
    float TLRafterSpace = (floatTLPMR * ispacing);
    float firstCreeper =  (TLRafterSpace + (mitreThickness/2))-(floatRafter/2);
    firstCreep.text = [[NSString alloc] initWithFormat:@"%.1f", firstCreeper];
    float comSetBack = floatTLPMR * (iridge/2);
    comRaftSetBack.text = [[NSString alloc] initWithFormat:@"%.1f", comSetBack];
    float crownSetBack = (floatTLPMR * (floatRafter/2));
    crownEndSetBack.text = [[NSString alloc] initWithFormat:@"%.1f", crownSetBack];
    creeperArray = [[NSMutableArray alloc] initWithCapacity:20];
for (int x = firstCreeper; x <= comraftbird; x + TLRafterSpace) {
[creeperArray addObject:[NSString stringWithFormat:@"%d", x]];
}
}

你陷入了一个无限循环,因为你忘记了递增x的值:

for (int x = firstCreeper; x <= comraftbird; x + TLRafterSpace)
                                             ^^^^^^^^^^^^^^^^^

那应该是:

for (int x = firstCreeper; x <= comraftbird; x += TLRafterSpace)

相关内容

  • 没有找到相关文章

最新更新