Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在android中使用JSON数组在测验中随机提问?_Java_Android_Json_Random - Fatal编程技术网

Java 如何在android中使用JSON数组在测验中随机提问?

Java 如何在android中使用JSON数组在测验中随机提问?,java,android,json,random,Java,Android,Json,Random,我正在开发一个测验,其中我存储了50个问题,我希望它在每次用户玩的测验中随机显示。我怎么做?是否可以使用json随机化问题?请帮助我。。我真的很感谢你的帮助。谢谢 public class Question1 extends Activity { Intent menu = null; BufferedReader bReader = null; static JSONArray quesList = null; /** Called when the activity is first c

我正在开发一个测验,其中我存储了50个问题,我希望它在每次用户玩的测验中随机显示。我怎么做?是否可以使用json随机化问题?请帮助我。。我真的很感谢你的帮助。谢谢

public class Question1 extends Activity {

Intent menu = null;
BufferedReader bReader = null;
static JSONArray quesList = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.question10);

    Thread thread = new Thread() {
        public void run() {
            try {
                Thread.sleep(1 * 1000);
                finish();
                loadQuestions();
                Intent intent = new Intent(Question1.this,
                        Question2.class);
                Question1.this.startActivity(intent);
            } catch (Exception e) {
            }
        }
    };
    thread.start();

}

private void loadQuestions() throws Exception {
    try {
        InputStream questions = this.getBaseContext().getResources()
                .openRawResource(R.raw.questions);
        bReader = new BufferedReader(new InputStreamReader(questions));
        StringBuilder quesString = new StringBuilder();
        String aJsonLine = null;
        while ((aJsonLine = bReader.readLine()) != null) {
            quesString.append(aJsonLine);
        }
        Log.d(this.getClass().toString(), quesString.toString());
        JSONObject quesObj = new JSONObject(quesString.toString());
        quesList = quesObj.getJSONArray("Questions");
        Log.d(this.getClass().getName(),
                "Num Questions " + quesList.length());
    } catch (Exception e) {

    } finally {
        try {
            bReader.close();
        } catch (Exception e) {
            Log.e("", e.getMessage().toString(), e.getCause());
        }

    }

}

public static JSONArray getQuesList(){

    return quesList;



}
}

公开课问题2扩展活动{
/**在首次创建活动时调用*/
TextView问题,items=null;
RadioButton answer1=空;
RadioButton answer2=空;
RadioButton answer3=空;
放射组答案=null;
int selectedAnswer=-1;
int quesIndex=0;
int numEvents=0;
int selected[]=null;
int correctAns[]=null;
布尔审查=假;
按钮next=null;
智力得分;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.startquiz);
试一试{
items=(TextView)findViewById(R.id.displayitems);
问题=(TextView)findViewById(R.id.displayquestion);
回答1=(单选按钮)查找视图id(R.id.option1);
answer2=(单选按钮)findViewById(R.id.option2);
回答3=(单选按钮)查找视图id(R.id.option3);
答案=(放射组)findViewById(R.id.QueGroup1);
下一步=(按钮)findViewById(R.id.selected);
next.setOnClickListener(nextListener);
selected=newint[Question1.getquesslist().length()];
java.util.array.fill(选中,-1);
correctAns=new int[Question1.getquestist().length()];
java.util.array.fill(correctAns,-1);
此.showQuestion(0,回顾);
}捕获(例外e){
Log.e(“”,e.getMessage().toString(),e.getCause());
}
}
私有void showQuestion(int-qIndex,boolean review){
试一试{
JSONObject aQues=Question1.getQuesList().getJSONObject(
qIndex);
字符串quesValue=aQues.getString(“问题”);
如果(更正[qIndex]=-1){
字符串correctAnsStr=aQues.getString(“CorrectAnswer”);
correctAns[qIndex]=Integer.parseInt(correctAnsStr);
}
question.setText(quesValue.toCharArray(),0,quesValue.length());
答案。勾选(-1);
回答1.setTextColor(颜色:黑色);
回答2.setTextColor(颜色:黑色);
回答3.setTextColor(颜色:黑色);
JSONArray ansList=aQues.getJSONArray(“答案”);
字符串aAns=ansList.getJSONObject(0.getString(“应答”);
答案1.setText(aAns.tocharray(),0,aAns.length());
aAns=ansList.getJSONObject(1.getString(“答案”);
答案2.setText(aAns.tocharray(),0,aAns.length());
aAns=ansList.getJSONObject(2.getString(“答案”);
答案3.setText(aAns.tocharray(),0,aAns.length());
Log.d(“”,选择[qIndex]+”);
如果(选择[qIndex]==0)
答案。检查(R.id.option1);
如果(选择[qIndex]==1)
答案。检查(R.id.option2);
如果(所选[qIndex]==2)
答案。检查(R.id.option3);
setText();
如果(quesIndex==(问题1.getQuesList().length()-1))
next.setEnabled(false);
如果(quesIndex<(问题1.getQuesList().length()-1))
next.setEnabled(true);
如果(审查){
Log.d(“审阅”,选择[qIndex]+“”+correctAns[qIndex]);
;
如果(已选择[qIndex]!=correctAns[qIndex]){
如果(选择[qIndex]==0)
回答1.setTextColor(颜色:红色);
如果(选择[qIndex]==1)
回答2.setTextColor(颜色:红色);
如果(所选[qIndex]==2)
回答3.setTextColor(颜色:红色);
}
if(correctAns[qIndex]==0)
回答1.setTextColor(颜色:绿色);
if(correctAns[qIndex]==1)
回答2.setTextColor(颜色为绿色);
if(校正[qIndex]==2)
回答3.setTextColor(颜色:绿色);
}
}捕获(例外e){
Log.e(this.getClass().toString(),e.getMessage(),e.getCause());
}
}
私有void setAnswer(){
如果(回答1.isChecked())
所选[quesIndex]=0;
如果(回答2.isChecked())
所选[quesIndex]=1;
如果(回答3.isChecked())
所选[quesIndex]=2;
Log.d(“,Arrays.toString(选中));
Log.d(“,array.toString(correctAns));
}
private OnClickListener nextListener=new OnClickListener(){
公共void onClick(视图v){
setAnswer();
对于(int i=0;i=Question1.getQuesList().length())
//quesIndex=Question1.getQuesList().length()-1;
showQuestion(quesIndex,review);
}
}
};
私有void setText(){
此.setTitle(“问题”+(quesIndex+1)+“超出”
+问题1.getQuesList().length());
设置重力(250);
}
公共空间重新加载(){
Intent=getIntent();
覆盖转换(0,0);
intent.addFlags(intent.FLAG\u活动\u无\u动画);
完成();
覆盖转换(0,0);
星触觉(意向);
}

}

使用
((int)Math.random()*)
为你的问题数组生成一个随机索引

但我建议你应该使用Colection.shuffle()函数,而不是Math.random()bcz有时问题会重复。这是我个人的意见…随机有它自己的局限性,它会在2^48次呼叫后重复。(它使用48位种子)。但这并不意味着它不够随机。@rajesh.adhi,有可能限制问题的显示吗?其中我存储了50个问题,然后我只需要10个问题来显示int index=(int)(Math.random()*10);当加载一个问题时,您必须将计数减少1,即加载一个问题后,计数应为int index=(int)(Math.random()*9);。另外,如果你想从第20个索引开始提问,你可以给出20+(int)(Math.random()*10);非常感谢……这是一个很大的帮助!
public class Question2 extends Activity {
/** Called when the activity is first created. */


TextView question, items = null;
RadioButton answer1 = null;
RadioButton answer2 = null;
RadioButton answer3 = null;
RadioGroup answers = null;
int selectedAnswer = -1;
int quesIndex = 0;
int numEvents = 0;
int selected[] = null;
int correctAns[] = null;
boolean review = false;
Button next = null;
int score;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.startquiz);


    try {

        items = (TextView)findViewById(R.id.displayitems);
        question = (TextView) findViewById(R.id.displayquestion);
        answer1 = (RadioButton) findViewById(R.id.option1);
        answer2 = (RadioButton) findViewById(R.id.option2);
        answer3 = (RadioButton) findViewById(R.id.option3);
        answers = (RadioGroup) findViewById(R.id.QueGroup1);


        next = (Button) findViewById(R.id.selected);
        next.setOnClickListener(nextListener);

        selected = new int[Question1.getQuesList().length()];
        java.util.Arrays.fill(selected, -1);
        correctAns = new int[Question1.getQuesList().length()];
        java.util.Arrays.fill(correctAns, -1);

        this.showQuestion(0, review);



    } catch (Exception e) {
        Log.e("", e.getMessage().toString(), e.getCause());
    }

}

private void showQuestion(int qIndex, boolean review) {
    try {
        JSONObject aQues = Question1.getQuesList().getJSONObject(
                qIndex);
        String quesValue = aQues.getString("Question");
        if (correctAns[qIndex] == -1) {
            String correctAnsStr = aQues.getString("CorrectAnswer");
            correctAns[qIndex] = Integer.parseInt(correctAnsStr);
        }

        question.setText(quesValue.toCharArray(), 0, quesValue.length());
        answers.check(-1);
        answer1.setTextColor(Color.BLACK);
        answer2.setTextColor(Color.BLACK);
        answer3.setTextColor(Color.BLACK);
        JSONArray ansList = aQues.getJSONArray("Answers");
        String aAns = ansList.getJSONObject(0).getString("Answer");
        answer1.setText(aAns.toCharArray(), 0, aAns.length());
        aAns = ansList.getJSONObject(1).getString("Answer");
        answer2.setText(aAns.toCharArray(), 0, aAns.length());
        aAns = ansList.getJSONObject(2).getString("Answer");
        answer3.setText(aAns.toCharArray(), 0, aAns.length());
        Log.d("", selected[qIndex] + "");
        if (selected[qIndex] == 0)
            answers.check(R.id.option1);
        if (selected[qIndex] == 1)
            answers.check(R.id.option2);
        if (selected[qIndex] == 2)
            answers.check(R.id.option3);

        setText();
        if (quesIndex == (Question1.getQuesList().length() - 1))
            next.setEnabled(false);

        if (quesIndex < (Question1.getQuesList().length() - 1))
            next.setEnabled(true);

        if (review) {
            Log.d("review", selected[qIndex] + "" + correctAns[qIndex]);
            ;
            if (selected[qIndex] != correctAns[qIndex]) {
                if (selected[qIndex] == 0)
                    answer1.setTextColor(Color.RED);
                if (selected[qIndex] == 1)
                    answer2.setTextColor(Color.RED);
                if (selected[qIndex] == 2)
                    answer3.setTextColor(Color.RED);
            }
            if (correctAns[qIndex] == 0)
                answer1.setTextColor(Color.GREEN);
            if (correctAns[qIndex] == 1)
                answer2.setTextColor(Color.GREEN);
            if (correctAns[qIndex] == 2)
                answer3.setTextColor(Color.GREEN);
        }
    } catch (Exception e) {
        Log.e(this.getClass().toString(), e.getMessage(), e.getCause());
    }
}


private void setAnswer() {
    if (answer1.isChecked())
        selected[quesIndex] = 0;
    if (answer2.isChecked())
        selected[quesIndex] = 1;
    if (answer3.isChecked())
        selected[quesIndex] = 2;



    Log.d("", Arrays.toString(selected));
    Log.d("", Arrays.toString(correctAns));

}

private OnClickListener nextListener = new OnClickListener() {
    public void onClick(View v) {
        setAnswer();

        for(int i=0; i<correctAns.length; i++){

            if ((correctAns[i] != -1) || (correctAns[i] == selected[i]))
            {
                if (correctAns[i] == selected[i])
                score++;
                Toast.makeText(getApplicationContext(), "Your answer is correct!", Toast.LENGTH_SHORT).show();
            }else
        {
            Toast.makeText(getApplicationContext(), "Your answer is wrong...", Toast.LENGTH_SHORT).show();
        }

        quesIndex++;
        if (quesIndex >= Question1.getQuesList().length())
            //quesIndex = Question1.getQuesList().length() - 1;

        showQuestion(quesIndex, review);
        }

    }
};
private void setText() {
    this.setTitle("Question     " + (quesIndex + 1) + "out of     "
            + Question1.getQuesList().length());
    items.setGravity(250);
}

public void reload() {

Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();

overridePendingTransition(0, 0);
startActivity(intent);
}