0

我正在做一个包含 10 个问题和答案的项目。我正在使用 touchesbegan、touchesmoved 和 touchesended 方法。问题出现在一个标签中,答案在屏幕上有 10 个不同的标签。用户应该匹配他们,基本上这就是游戏。在 touchesended 方法中,我正在使用 if 语句验证问题的结果和答案的位置,因此请尝试通过拖动到正确的位置来了解它是否是正确的答案。

所以,我需要问这个;当用户给出正确答案时,问题应该改变。我对 NSMutableArray 提出了问题,每次打开它的顺序都不同且随机。如何在相同的标签和文本中显示数组的其他对象。(它应该是文本,因为后面还有另一个数字)这是一些代码的示例:

arr = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil];

srandom(time(NULL));
NSUInteger count = [arr count];
for (NSUInteger i = 0; i < count; ++i) {
    int nElements = count - i;
    int n = (random() % nElements) + i;
    [arr exchangeObjectAtIndex: i withObjectAtIndex:n];
}
Question.text = [NSString stringWithFormat:@"%ix1=", [[arr objectAtIndex:(random()%9)+1] intValue]];

问题解决后,我需要带来数组的其他数字之一。也使用过的数字不应该再次出现。

任何人都可以帮忙吗?

4

1 回答 1

0

代替

Question.text = [NSString stringWithFormat:@"%ix1=", [[arr objectAtIndex:(random()%9)+1] intValue]];

您需要先获取随机索引号

int index = (random()%9) + 1;

然后

Question.text = [NSString stringWithFormat:@"%ix1=", [[arr objectAtIndex:index] intValue]];

接下来就可以使用index删除对应的对象了NSMutableArray

[arr removeObjectAtIndex:index];
于 2012-07-18T16:25:22.353 回答