2013-04-30 21 views
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」

我把它這意味着我不應該使用另一種陣列被存儲隨機問題,因爲我不能用這個從數組中移除問題。但我不知道如何做到這一點?

我完全誤解我應該怎麼做呢?

回答

1

因爲在這裏你的目標是讓非重複的問題...

我相信,而不是刪除你已經使用的問題,您應該在開始遍歷數組一個洗你的數組,然後循環索引在一次使用一個簡單的計數器。

我希望你能找到這段代碼有幫助的 - 給它一個鏡頭:

-(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]; 
} 

你最終將具有洗牌每次完全不同的問題。

我希望你覺得這有幫助。

+0

嗨,謝謝你,這似乎工作。我唯一的問題是,我不能讓它增加一次以上(我添加了更多的問題)。我不確定移動到數組中下一個索引的正確方法。我已經嘗試把下面的按鈕,但只適用於第一次點擊'for(i = 0; i <[_questionArray count]; i ++){ – user1759949 2013-05-02 22:39:01

+0

您好,我'我編輯了我的答案,並添加了一個函數,可以幫助您遍歷數組的索引。您只需添加上面的代碼並將操作附加到按鈕上即可完成設置。 – 2013-05-03 22:39:09

+0

這很好。非常感謝您的幫助。 – user1759949 2013-05-04 10:34:22