创建一系列不重复的测验问题

时间:2013-04-30 21:01:48

标签: xcode random nsmutablearray

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

- (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'类型的参数

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

我完全误解了我应该怎么做?

1 个答案:

答案 0 :(得分: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];
}

你最终会得到完全不同的问题,这些问题每次都会被洗牌。

我希望你觉得这很有用。