-1

クイズアプリを作ろうとしていますが、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;}
4

2 に答える 2

0

6 つの不正解の配列があり、そのうちの 3 つをランダムに選択したい場合

NSArray *answers = [NSArray arrayWithObjects:@"Wrong #1", @"Wrong #2", @"Wrong #3", @"Wrong #4", @"Wrong #5", @"Wrong #6"];
NSMutableArray *output = [[NSMutableArray alloc] init];
int r;
while (output.count < 3) {
r = arc4random_uniform(5);
if (![output containsObject:[answers objectAtIndex:r]]) {
[output addObject:[answers objectAtIndex:r]];
}}
于 2013-08-14T18:47:11.853 に答える