现在我已经设法在我的按钮上显示随机文本,我注意到大多数时候数组中的项目会重复自身。所以我得到类似“abb b”或“acb c”的东西,而不是“abc d”或“acd b”。如何告诉我的 for 循环排除已使用的数组值的索引,这样它就不会重复任何内容?
另外,我如何告诉 for 循环从数组中的某些值而不是从所有值中进行选择,这样我就不必为测验中的每个问题制作一个数组列表?就像我的数组中有 [abcdefg] 一样,我希望问题 1 仅以随机顺序显示 [abce]。
这是我当前的代码的样子:
answerList = [[NSMutableArray alloc] initWithObjects:
"a", "b", "c", "d", "e", "f", "g", nil];
for (int j=0; j<answerList.count; j++)
{
int k = arc4random() % [answerList count];
[btnA setTitle:[answerList objectAtIndex:k] forState:UIControlStateNormal];
[answerList removeObjectAtIndex:k];
int l = arc4random() % [answerList count];
[btnB setTitle:[answerList objectAtIndex:l] forState:UIControlStateNormal];
[answerList removeObjectAtIndex:l];
int m = arc4random() % [answerList count];
[btnC setTitle:[answerList objectAtIndex:m] forState:UIControlStateNormal];
[answerList removeObjectAtIndex:m];
int n = arc4random() % [answerList count];
[btnD setTitle:[answerList objectAtIndex:n] forState:UIControlStateNormal];
[answerList removeObjectAtIndex:n]; }