比较 3 个数组。测验应用程序。如何先显示错误答案?

发布于 2025-01-07 08:44:09 字数 979 浏览 4 评论 0原文

我有三个数组:

NSArray *Q = [1+1, 2+2, 3+3, 4+4, 5+5]; 
NSArray *A = [2, 4, 6, 8, 10]; 
NSArray *UserAnswers = [2, 4, 10, 17, 26]; 

在表格视图中,我想显示结果,但错误答案(在本例中为 10,17,26)必须在正确答案(在表格视图顶部)之前显示。

我将其发布在这里是因为我需要一个干净而智能的解决方案来解决这个问题。有什么想法吗?

这是肮脏的方式,有人知道更简单/更干净的方式吗?

 NSMutableArray *wrongs = [NSMutableArray new];
NSMutableArray *rights = [NSMutableArray new];
NSMutableArray *dataSource = [NSMutableArray new]; 

for (int x = 0; x<questions.count; x++) {
    NSDictionary *temp = [[NSDictionary alloc]initWithObjectsAndKeys:[questions objectAtIndex:x],@"q",[answers objectAtIndex:x],@"a",[userAnswers objectAtIndex:x], "ua", nil];

    if ([[userAnswers objectAtIndex:x]isEqual:[answers objectAtIndex:x]]) 
        [rights addObject:temp]; 
    else
        [wrongs addObject:temp]; 
}

[dataSource addObjectsFromArray:wrongs]; 
[dataSource addObjectsFromArray:rights]; 

[wrongs release]; 
[rights release]; 

I have three arrays:

NSArray *Q = [1+1, 2+2, 3+3, 4+4, 5+5]; 
NSArray *A = [2, 4, 6, 8, 10]; 
NSArray *UserAnswers = [2, 4, 10, 17, 26]; 

In a tableview I want to show the results but the wrong answers(in this case 10,17,26) must be shown before the correct answers(At top of the tableView).

I post it here because I need a clean and smart solution to solve this. Any ideas?

this is the dirty way, Anyone know an easier/cleaner way?

 NSMutableArray *wrongs = [NSMutableArray new];
NSMutableArray *rights = [NSMutableArray new];
NSMutableArray *dataSource = [NSMutableArray new]; 

for (int x = 0; x<questions.count; x++) {
    NSDictionary *temp = [[NSDictionary alloc]initWithObjectsAndKeys:[questions objectAtIndex:x],@"q",[answers objectAtIndex:x],@"a",[userAnswers objectAtIndex:x], "ua", nil];

    if ([[userAnswers objectAtIndex:x]isEqual:[answers objectAtIndex:x]]) 
        [rights addObject:temp]; 
    else
        [wrongs addObject:temp]; 
}

[dataSource addObjectsFromArray:wrongs]; 
[dataSource addObjectsFromArray:rights]; 

[wrongs release]; 
[rights release]; 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

緦唸λ蓇 2025-01-14 08:44:09

来自评论

你的方式看起来相当干净;也许在用户回答时将答案添加到正确的数组中?

From comments

Your way looks reasonably clean; perhaps add answers to the correct arrays as the user answers?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文