我正在尝试制作一个测验应用程序,但我是 IOS 新手。我想从一个特定问题的 6 个错误答案的数组中得到 3 个错误答案,每个问题都有 1 个正确答案。我想随机化错误每次显示问题时的选项。因此,每次显示问题时,应用程序都会从数组中选择 3 个错误选项和 1 个正确答案。我该如何编码?如果您能帮助我,我会很高兴。
谢谢..
{
int counted = [theOptions count];
int i;
NSMutableArray *indexes = [[NSMutableArray alloc] initWithCapacity:counted];
for (i=0; i<counted; i++) [indexes addObject:[NSNumber numberWithInt:i]];
NSMutableArray *shuffle = [[NSMutableArray alloc] initWithCapacity:counted];
while ([indexes count])
{
int index = rand()%[indexes count];
[shuffle addObject:[indexes objectAtIndex:index]];
[indexes removeObjectAtIndex:index];
}
NSMutableArray* shuffledOptions = [[NSMutableArray alloc] initWithCapacity:4];
for (int i=0; i<counted; i++)
{
int randomIndex = [[shuffle objectAtIndex:i] intValue];
[shuffledOptions addObject:[theOptions objectAtIndex:randomIndex]];
UIButton* optionButton = [_optionsButtonsArray objectAtIndex:i];
optionButton.tag = randomIndex;
}
return shuffledOptions;
}
return theOptions;}