Android 分配答案-测验计划
我需要一种方法在ArrayList中为一个问题指定3个可能的答案,其中只有一个答案是正确的 我是Android新手,所以如果你能帮我,我将不胜感激 我这里有一个问题课:Android 分配答案-测验计划,android,mobile,android-intent,Android,Mobile,Android Intent,我需要一种方法在ArrayList中为一个问题指定3个可能的答案,其中只有一个答案是正确的 我是Android新手,所以如果你能帮我,我将不胜感激 我这里有一个问题课: package com.example.quiz; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.os.CountDownTimer; import android.widge
package com.example.quiz;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.TextView;
public class Questions extends Activity {
private ArrayList<String> questionsArray = new ArrayList<String>();
public Questions() {
addQuestion("Which Prophet was the first in Islam?");
addQuestion("What is the purpose of life?");
addQuestion("Who is the last Prophet in Islam?");
}
public void addQuestion(String question) {
questionsArray.add(question);
}
public ArrayList<String> getQuestionsArray() {
return questionsArray;
}
public void setQuestionsArray(ArrayList<String> questionsArray) {
this.questionsArray = questionsArray;
}
}
package.com.example.quick;
导入java.util.ArrayList;
导入android.app.Activity;
导入android.os.Bundle;
导入android.os.CountDownTimer;
导入android.widget.TextView;
公共课堂提问扩展了活动{
private ArrayList questionsArray=new ArrayList();
公众问题(){
addQuestion(“哪位先知是伊斯兰教的第一位?”);
添加问题(“生活的目的是什么?”);
addQuestion(“谁是伊斯兰教最后一位先知?”);
}
公共问题(字符串问题){
问题补充(问题);
}
公共阵列列表getQuestionsArray(){
返回问题;
}
公共无效设置问题数组(ArrayList问题数组){
this.questionsArray=questionsArray;
}
}
有许多可能的方法。其中之一是为每个答案选项(即A、B和C)创建一个ArrayList
。然后再创建一个ArrayList
,指示哪个答案是正确的
因此,总共将有4个
ArrayList
s,3个用于存储答案,一个用于指示哪个选项正确当必须将数据与其他数据关联时,地图是有用的。在您的案例中,您试图将问题与每个问题的答案数组相关联
您可以制作一个HashMap-answers
并保留所有问题的答案(ArrayList
)(字符串),然后在另一个HashMap-correctAnswers
中保留问题(第一个String
)及其关联的正确答案(第二个String
)
验证答案时,您必须获得用户选择的答案,并将其与正确答案进行比较,如下所示:
if(correctAnswers.get(questionString).compareTo(chosenAnswerString))
{
//correct answer!
}
else
{
//incorrect answer
}
用于添加问题及其答案的方法您将成为:
[...]
private Map answers = new HashMap<String, ArrayList<Sting>>();
private Map correctAnswers = new HashMap<String, String>();
public void addQuestion(String question, ArrayList<String> answers, String correctAnswer)
{
this.answers.put(question, answers);
this.correctAnswers.put(question, correctAnswer);
}
[...]
[…]
私有映射应答=新HashMap();
private Map correctAnswers=新HashMap();
public void addQuestion(字符串问题、数组列表答案、字符串更正答案)
{
这个。答案。把(问题,答案);
这个。正确答案。把(问题,正确答案);
}
[...]
如上所述创建答案地图时,地图将保留问题数组、答案数组以及每个问题与其答案数组之间的关联。哦,对不起,没有在javadoc中查找地图,而是使用集合方法。尝试使用this.answers.put(问题,答案)
。我已经更新了答案并更正了错误+谢谢你指出这一点