0

我正在尝试创建一个不会重复问题的基本测验应用程序。我看过几个例子,相信我应该将问题存储在一个数组中,然后在每次使用时从数组中删除一个。我试过下面的代码。

- (void)viewDidLoad
{
//The array of questions
NSMutableArray *questionArray = [[NSMutableArray alloc] initWithObjects: 
    [NSMutableArray arrayWithObjects:@"First Question",@"Answer A", nil],
    [NSMutableArray arrayWithObjects:@"Second Quesiton",@"AnswerA",@"AnswerB", nil],
                                 nil];


//remove used question from array
for (int i = questionArray.count; i>=0; --i) {
    _questions = [questionArray objectAtIndex:arc4random() % questionArray.count];

    [questionArray exchangeObjectAtIndex:i withObjectAtIndex:_questions];
}

//use array object
self.lblQuestion.text = [_questions objectAtIndex:0];
[self.btnA setTitle:[_questions objectAtIndex:1] forState:UIControlStateNormal];

[super viewDidLoad];
}

我收到以下警告:指向整数转换的不兼容指针将 NSMutableArray*_strong 发送到“NSUInteger”类型的参数

我认为这意味着我不应该使用另一个数组来存储随机问题,因为我不能使用它来从数组中删除问题。但是我不知道该怎么做?

我完全误解了我应该如何去做吗?

4

1 回答 1

1

由于您的目标是获得不重复的问题...

我相信,与其删除您已经使用过的问题,不如在开始时对数组进行 SHUFFLE,然后使用简单的计数器一次循环遍历数组一个索引。

我希望你能发现这段代码有帮助——试一试:

-(NSMutableArray *)shuffleArray:(NSMutableArray *)anArray
    NSUInteger count = [anArray count];
    for (NSUInteger i = 0; i < count; ++i) 
    {
        int nElements = count - i;
        int n = (arc4random() % nElements) + i;
        [anArray exchangeObjectAtIndex:i withObjectAtIndex:n];
    }

    return anArray;
}

-(void)viewDidLoad
{
    //The array of questions
    NSMutableArray *questionArray = [[NSMutableArray alloc] initWithObjects: 
    [NSMutableArray arrayWithObjects:@"First Question",@"Answer A", nil],
    [NSMutableArray arrayWithObjects:@"Second Quesiton",@"AnswerA",@"AnswerB", nil],
                             nil];

    //Shuffle the question array -- Now all indexes are shuffled and Random
    questionArray = [self shuffleArray:questionArray];

    //Every time you want to move to the next question, all you have to do is connect a button to the nextIndex action and let it do all the work!
    //Use the nextIndex method to initialise -- we call it manually the first time so things would get going and something is displayed -- you can remove the line below if you want it to initialise on first button click! Your call the shots sir!
    [self nextIndex];


    [super viewDidLoad];
}

//Edit -- This method shows how to change question array index using a method
int currentIndex = 0;
-(IBAction)nextIndex
{
    if ( currentIndex == [questionArray count] )
    {
        currentIndex = 0; //Resets the var when getting to the final Index
        //The above line will result in a question loop -- meaning if you arrive at the last question, the next question will be the first! Pacman mode!
        //If you want to stop at the last question just change the above line to return; and you're all set!
    }

    //Set _questions object to the current Index Array element
    _questions = [questionArray objectAtIndex:currentIndex];

    //Increment currentIndex for next use
    currentIndex++;

    //use the array object to set your objects' values
    self.lblQuestion.text = [_questions objectAtIndex:0];
    [self.btnA setTitle:[_questions objectAtIndex:1] forState:UIControlStateNormal];
}

你最终会遇到完全不同的问题,每次都会被打乱。

我希望你觉得这有帮助。

于 2013-05-01T21:55:41.997 回答