繁体   English   中英

如何使用Json数组计算用户在测验中回答的项目数

[英]how to count number of items that users answered in the quiz using Json array

我正在开发一个简单的测验,其中存储了50个问题,并且随机显示。.我的问题是..我如何计算用户回答测验的项目数。例如,我有50个问题,如果用户只回答10个? 所以我希望它在我的成绩(对话框)中显示玩家的成绩和用户回答的项目数量。请帮助我..请..我该怎么做?帮助真的非常感谢!

private OnClickListener finishListener = new OnClickListener() {
        public void onClick(View v) {
            setAnswer();
            //Calculate Score
            int score = 0;
            int count = 0;

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

                if ((correctAns[i] != -1) && (correctAns[i] == selected[i]))
                    score++;


            }
            count++;
            AlertDialog alertDialog;
            alertDialog = new AlertDialog.Builder(Question2.this).create();
            alertDialog.setTitle("Your Score");
            alertDialog.setMessage("You've got "+(score)+" out of " + (count) + "  items");

            alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE,"Okay", new DialogInterface.OnClickListener(){

                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(Question2.this,
                            MainMenu.class);
                    startActivity(intent);
                }
            });

            alertDialog.show();

        }
    };

您可以创建一个隐藏变量,并在用户每次选择答案时对其进行递增。 当决定显示结果时,您可以从该隐藏变量中读取答案。

如果您永远需要保存的问题,我的意思是当用户退出程序并保存问题时。 在这种情况下,您可以将其保存在数据库的某个表中。 如果仅在此用户会话中需要保存的答案,则创建一些ArrayList并将其放在此问题列表中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM