我有以下代码,我想用它来检查用户答案并输出分数(满分5分)。我使用带有答案的plist并检查textField.text。我正在努力的是:如何使用这种方法获得总分输出分数?
- (IBAction)checkAnswers:(UITextField *)textField
{
NSString *path2 = [[NSBundle mainBundle] pathForResource:@"7A Cells Microscopes 3" ofType:@"plist"];
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:path2];
NSString *tester = [NSString stringWithFormat:@"%i", textField.tag];
// NSDictionary *secondDict = [dictionary valueForKey:tester];
// NSString *answer = [secondDict valueForKey:@"Answer"];
// if ([textField.text isEqualToString:[[dictionary valueForKey:tester] valueForKey:@"Answer"]]) {
// NSLog(@"YAY");
// }
NSArray *allTextFields = [[NSArray alloc] initWithObjects:eyepiece, objectiveLens, focussingKnobs, stage, mirror, nil];
for (textField in allTextFields) {
int x = 0;
if ([textField.text isEqualToString:[[dictionary valueForKey:tester] valueForKey:@"Answer"]]) {
x++;
NSLog(@"%i", x);
}
}
任何帮助将不胜感激! 非常感谢。
答案 0 :(得分:3)
假设您的其余代码都很好,只需在for循环外移动 int x = 0; 。你编码x的方式在每个循环中重置为0 ......所以它永远不会计数。