0

我正在尝试显示具有不同数字因子(“素数”)的数组。但是我总是得到 0,1,2,3,4,5,... 而不是给我 int 数字。

        factors.text = @"";

        int factorsNumber;

        NSMutableArray *array;
        array = [NSMutableArray arrayWithCapacity:5];

        for (factorsNumber=1; factorsNumber<=prime; factorsNumber++) {
            if (prime%factorsNumber == 0) {
                [array addObject:[NSString stringWithFormat:@"%d", factorsNumber]];
            }

        }


        for (int i = 0; i < [array count]; i++) {

            [array replaceObjectAtIndex:1 withObject:@"4"];

            NSString *temp = [NSString stringWithFormat:@"%d, ", i, [[array objectAtIndex:i] intValue]];
            factors.text = [factors.text stringByAppendingString:temp];
        }
4

1 回答 1

1

代替

NSString *temp = [NSString stringWithFormat:@"%d, ", i, [[array objectAtIndex:i] intValue]];

NSString *temp = [NSString stringWithFormat:@"%d, ", [[array objectAtIndex:i] intValue]];

问题是您只打印数组索引,而不是值。

于 2012-07-25T13:12:29.493 回答