多项和单项选择测试

时间:2012-12-01 07:02:56

标签: java android

我正在进行单项选择和多项选择测试。

我对每个问题都有几个问题和4个答案。

我正在改变答案,因为每个答案都分配给单选按钮。 这就是我如何改组arraylist,其中Random是一个带有项目的arraylist,而r1,r2,r3,r4是单选按钮。

random.add(val);
Collections.shuffle(random);


r1.setText(random.get(0));
r2.setText(random.get(1));
r3.setText(random.get(2));
r4.setText(random.get(3));

我能够以混乱的方式显示答案,但是当我选择答案时,我需要表明答案是对还是错。

Sample question and options.
1. which language is used for android programming?
 A.PHP
 B.JAVA
 C.C
 D.C++

正确答案是B我需要显示正确答案是B。

如何实现这一点。

修改 我试过这个:

每个单选按钮的Onclick指定值A,如果正确显示正确,则将值与xml值进行比较,但当我混杂时,它将不起作用。

编辑2 XML

<Question no="1" text="Which Programming language is used in android develoment" type="SCA" noc="4" jumble="NO" correctans="PHP">
<choice a = "PHP" flag="A"> 
<choice b = "JAVA" flag="B"> 
<choice c = "C" flag="C"> 
<choice d = "C++" flag="D"> 

8 个答案:

答案 0 :(得分:6)

您可以使用Option-isOptionCorrect对创建一个hashmap。喜欢你的情况:

HashMap<String, Boolean> choices = new HashMap<String, Boolean>();
choices.put("PHP", false);
choices.put("JAVA", true);
choices.put("C", false);
choices.put("C++", false);

现在随机播放键值对。您正确的选择将是在HashMap中具有值true的那个。

答案 1 :(得分:4)

Egor很清楚他的建议,但我会让你使用你当前的实现。

class Option{
      //You can add any other parameters if required.
      String optionText;
      boolean isAnswer;
}

// Use arraylist of Option class like this ArrayList<Option> options = new ArrayList<Option>(); // in your case random

ArrayList<Option> options = new ArrayList<Option>();

希望这会对你有所帮助。

答案 2 :(得分:2)

这里最好的方法不是与Strings一起操作,而是创建一个Question类,它将包含有关问题的所有信息:其值,答案列表和索引。正确答案。解析XML时,创建Question个对象的列表,然后使用它们。不再存在任何映射问题。希望这会有所帮助。

答案 3 :(得分:2)

这是一个有点天真的解决方案,但它应该有用。

class Question {
    String message;
    String answer;
    List<String> options; //"Java", "PHP", etc
}

在Question对象中随机播放Map的键 在单选按钮中,执行r1.setText(random.get(0))

之类的操作

点击,执行

String choice = null;
for (RadioButton rb : rBtns) {
    if (rb.isSelected) {
        choice = rb.getText(); break();
    }
}

if (choice.equals(question.getAnswer))
    return true; //correct
else
    return false; //wrong

答案 4 :(得分:2)

您可以在模型中的“随机”arraylist中保留答案,而不是纯粹的字符串;

private class AnswerModel {
    string answer;
    boolean flag;

//... getters and setters...
}

在这里,您可以将您的真实答案标志设置为true,将所有其他标志设置为false。这样,如果答案是正确的,你可以简单地返回。

答案 5 :(得分:1)

这里已有好几个答案。但另一种选择是编写自己的shuffle函数。 shuffle算法是一种非常简单的算法,它在线性时间内运行,至少对于数组而言。通过编写自己的随机播放功能,您可以跟踪正确答案的结束位置。

为了简单起见,我发布了一个代码,该代码返回一个混洗集合中指定索引的新索引。该函数改变(改变)原始集合,因此这应该有效。

/**
 * @return Returns the new index of the element that was placed at correctIndex,
 *         or -1 if the correctIndex parameter was out of bounds.
 */
public int shuffleAnswers(Collection<String> collection, index correctIndex) {
    String[] shuffleArray = new String[collection.size()];
    int returnValue = -1;

    collection.toArray(shuffleArray); // Convert to array
    collection.clear(); // We have to add the elements again later

    // Pick random elements
    for (int i = shuffleArray.length; i > 0; i--) {
        int randIndex = Math.random() * i;
        if (returnValue == -1 && randIndex == correctIndex) {
            // This only works if elements are added to the end
            // So you may want to limit the function to ArrayLists or LinkedLists
            returnValue = collection.size();
        }

        // Add the randomly selected element to the collection
        collection.add(shuffleArray[randIndex]);

        // We must ensure that we don't lose elements
        // So we swap them down from the end
        shuffleArray[randIndex] = shuffleArray[i - 1];
    }

    return returnValue;
}

注意注释,这只适用于将元素添加到集合末尾的集合,并且从添加到集合的第一个元素到最后一个元素填充数组。

这可能比其他解决方案慢,但请注意,无论如何都需要随机播放,因此它不应该影响运行速度。

答案 6 :(得分:1)

另一种选择就是拥有correctAnswer字符串,然后将用户的选择与字符串(.equals())进行比较。

答案 7 :(得分:0)

这就是我要做的。 由于您在XML文件中记住答案而不是答案位置,因此在选定的单选按钮上取文本(在本例中为“PHP”,“Java”,“C ++”或“C”)并将其与正确答案进行比较

Jumbling不会对此产生影响,否则你做错了。