1

我正在尝试在 iOS 上继续我的问答游戏,但我遇到了问题。我成功地创建了 sqlite 数组和随机问答,但现在我无法想象如何创建 answer 方法。在我的每个问题的 sqlite 表中,answer1 列都是正确答案。那么如何创建一种方法来使用按钮检查正确答案呢?以上是随机问题和随机答案代码。

 -(NSMutableArray *) categoriesList{
categories = [[NSMutableArray alloc] initWithCapacity:10];
@try {
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"Categories.sqlite"];
    BOOL success = [fileMgr fileExistsAtPath:dbPath];
    if(!success)
    {
        NSLog(@"Cannot locate database file '%@'.", dbPath);
    }
    if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
    {
        NSLog(@"An error has occured: %s", sqlite3_errmsg(db));

    }


    const char *sql = "SELECT * FROM Questions WHERE Category IS 2 ORDER BY RANDOM() LIMIT 1";
    sqlite3_stmt *sqlStatement;
    if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
    {
        NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
    }else{

        while (sqlite3_step(sqlStatement)==SQLITE_ROW) {

            Categories * choise = [[Categories alloc] init];
            question.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 1)];
            NSMutableArray  *arary = [[NSMutableArray alloc]init];
            while (arary.count < 4) {
                int value = arc4random()%4+2;
                BOOL isFound = [[arary filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"intValue == %d",value]]] count];
                if(!isFound)
                    [arary addObject:[NSNumber numberWithInteger:value]];                   
            }
            answer1.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, [[arary objectAtIndex:0] intValue])];
            answer2.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, [[arary objectAtIndex:1] intValue])];
            answer3.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, [[arary objectAtIndex:2] intValue])];
            answer4.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, [[arary objectAtIndex:3] intValue])];

            [categories addObject:choise];

        }
    }
    sqlite3_finalize(sqlStatement);
}
@catch (NSException *exception) {
    NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
}
@finally {

    sqlite3_close(db);

    return categories;
}

这是我尝试创建一个答案方法。

- (IBAction)answer1:(id)sender {    
if([answer1.text isEqualToString:@"2"])
{
    NSLog(@"Correct");
}else{
    NSLog(@"Incorrect");
}
}

- (IBAction)answer2:(id)sender {    
if([answer2.text isEqualToString:@"2"])
{
    NSLog(@"Correct");
}else{
    NSLog(@"Incorrect");
}
}
- (IBAction)answer3:(id)sender {    
if([answer3.text isEqualToString:@"2"])
{
    NSLog(@"Correct");
}else{
    NSLog(@"Incorrect");
}
}
- (IBAction)answer4:(id)sender {
if([answer4.text isEqualToString:@"2"])
{
    NSLog(@"Correct");
}else{
    NSLog(@"Incorrect");
}
}

有人可以帮助如何使用四个按钮创建正确的答案方法吗?

4

1 回答 1

0

您的问题应该更具描述性,但据我了解,您的数据库不是结构化的。您必须在 Db 上使用规范化。Like Tables 结构应该是这样的: tablename: Question(questionId,QuestionTitle,CorrectOptionId,categoryId) tableName: Option(optionId,QuestionId,OptionTitle) To pic random question query 应该是:@"SELECT * FROM Question WHERE Category IS 2 ORDER BY RANDOM () LIMIT 1" 您可以将模型类用于问题。所以要从数据库中获取答案:@"select * from option where questionId = %d",questionObj.questionId。你可以洗牌你的选项数组。为选项创建随机按钮并设置 optionBTN.tag = optionObj.optionId。并单击 optionBTN 比较 optionBTN.tag == questionObj.CorrectOptionId ,如果相等则表示它是正确的。

我想你会明白我的逻辑的。我已经在一个项目中实现了它,它工作正常。如果您有任何疑问,请告诉我。

编辑:我认为这段代码会帮助你:

NSMutableArray  *arary = [[NSMutableArray alloc]init];
    while (arary.count < 4) {
        int value = arc4random()%4+2;
        BOOL isFound = [[arary filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"intValue == %d",value]]] count];
        if(!isFound)
            [arary addObject:[NSNumber numberWithInteger:value]];
    }

更新: 替换此代码:

while (sqlite3_step(sqlStatement)==SQLITE_ROW) {

            Categories * choise = [[Categories alloc] init];
            question.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 1)];
            NSMutableArray  *arary = [[NSMutableArray alloc]init];
            while (arary.count < 4) {
                int value = arc4random()%4+2;
                BOOL isFound = [[arary filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"intValue == %d",value]]] count];
                if(!isFound)
                    [arary addObject:[NSNumber numberWithInteger:value]];
                answer1.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, value)];
                answer2.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, value)];
                answer3.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, value)];
                answer4.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, value)];
            }

经过:

while (sqlite3_step(sqlStatement)==SQLITE_ROW) {

            Categories * choise = [[Categories alloc] init];
            question.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 1)];
            NSMutableArray  *arary = [[NSMutableArray alloc]init];
            while (arary.count < 4) {
                int value = arc4random()%4+2;
                BOOL isFound = [[arary filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"intValue == %d",value]]] count];
                if(!isFound)
                    [arary addObject:[NSNumber numberWithInteger:value]];
          }
               //place this code outside while loop.
                answer1.text = [NSSTring stringWithFormat:@"%d",[[array objectAtIndex:0] intValue]];//you can use exception handling here.
                answer2.text = [NSSTring stringWithFormat:@"%d",[[array objectAtIndex:1] intValue]];;
                answer3.text = [NSSTring stringWithFormat:@"%d",[[array objectAtIndex:2] intValue]];
                answer4.text = [NSSTring stringWithFormat:@"%d",[[array objectAtIndex:3] intValue]];
于 2013-05-25T15:57:47.553 回答