我已經做了一個測驗,並希望它能夠隨機應變,使其更具挑戰性。這意味着當應該有一種方法可以檢查哪個按鈕是答案。如果答案是按鈕1,則按鈕1包含正確的答案。如果答案是按鈕4,則按鈕4包含正確的答案等。Shuffle在iPhone應用程序測驗中的答案
現在,代碼每當我玩一款新遊戲時都洗牌答案,但它將所有4個按鈕的標題設置爲相同標題和正確答案總是按鈕1.
任何想法?
菲利普
這就是我所擁有的。
- (IBAction)PlayHistory_Easy:(id)sender {
// The questions
history_Easy = [[NSMutableArray alloc] initWithObjects:kHisQ_E1, kHisQ_E2, kHisQ_E3, kHisQ_E4,nil];
numberOfQuestions = [history_Easy count];
index = arc4random() % numberOfQuestions--;
NSString *dd = [history_Easy objectAtIndex:index];
questionLabel.text = dd;
// Setup code to shuffle answers
NSMutableArray *randomizeAnswer = [[NSMutableArray alloc] initWithObjects:btnOne, btnTwo, btnThree, btnFour, nil];
NSMutableArray *ranAns = [[NSMutableArray alloc] initWithObjects:@"Answer 1", @"Answer 2", @"Answer 3", @"Answer 4", nil];
if ([questionLabel.text isEqualToString:kHisQ_E1]) { // Answer 1st button
for (UIButton *btn in randomizeAnswer) {
int i = arc4random() % ranAns.count;
NSString *str = [ranAns objectAtIndex:i];
[btnOne setTitle:[ranAns objectAtIndex:i] forState:UIControlStateNormal];
[btnTwo setTitle:[ranAns objectAtIndex:i] forState:UIControlStateNormal];
[btnThree setTitle:[ranAns objectAtIndex:i] forState:UIControlStateNormal];
[btnFour setTitle:[ranAns objectAtIndex:i] forState:UIControlStateNormal];
}
}
查看答案。在這裏檢查答案是手動完成的,但爲了打亂答案,代碼顯然不同。
- (IBAction)button1:(id)sender {
if ([questionLabel.text isEqualToString:kHisQ_E1]) {
[self playRIGHTAnswer_EASY];
}
}
- (IBAction)button2:(id)sender {
if ([questionLabel.text isEqualToString:kHisQ_E1]) {
[self playWRONGAnswer_EASY];
}
}
- (IBAction)button3:(id)sender {
if ([questionLabel.text isEqualToString:kHisQ_E1]) {
[self playWRONGAnswer_EASY];
}
}
- (IBAction)button4:(id)sender {
if ([questionLabel.text isEqualToString:kHisQ_E1]) {
[self playWRONGAnswer_EASY];
}
}
謝謝Wain。我會試試看。 –