I am writing a small app, like an English multiple choices question test . I have a lot of questions, each question has few options, there is only one correct answer.
User choose an option to be an answer, then press next to display another question.
I can do until knowing whether the user select wrong or right answer already. But finish the question. I don’t know, how to calculate how many right or wrong answers. Because the user can go back to change the answer selection. Here is the code, Please help me to modify to the right code.
-(IBAction) checkButtonPressed:(UIButton *) sender{
for(int i=0;i<[self.arrayButtons count];i++){
[[self.arrayButtons objectAtIndex:i] setImage:[UIImage imageNamed:@"chkUnChecked.png"] forState:UIControlStateNormal];
}
NSLog(@"sender tag: %d", sender.tag);
[sender setImage:[UIImage imageNamed:@"chkChecked.png"] forState:UIControlStateNormal];
TrainEnglishAppDelegate *delegate = (TrainEnglishAppDelegate*)[UIApplication sharedApplication].delegate;
NSDictionary *item = [delegate.fullQuestion objectAtIndex:z];
NSString *correctAnswer = [item objectForKey:@"correct"];
NSArray *answers = [item objectForKey:@"answers"];
NSString *answer = [answers objectAtIndex:sender.tag];
if ([answer isEqualToString:correctAnswer]) {
++numberCorrect;
NSLog(@"did correct");
} else
{
if (numberCorrect > 0) {
--numberCorrect;
} else
{
++numberWrong;
}
NSLog(@"did wrong");
}
}
PS: if the user cannot go back to fix their selection, it is much easier. But the requirement say so. I am not sure now. Please help.
I’ve done something similar (though more complex), and I’ve found that the only reasonable way to do it is to keep a “scorecard” with all of the answers and not generate a “score” until the end, at which time I scan back through the “scorecard” and add things up. Trying to keep a “running score”, while possible, just isn’t a robust way to do it.