简体   繁体   中英

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

im developing a simple quiz where in have 50 questions stored and it is randomly displayed..my problem is..how can i count the number of items that the user answered the quiz..for example, i have 50 questions, what if the user only answer 10? so i want it to display in my score(dialogbox) the score of the player and the number of items that the user answered.please help me..please..how can i do that???help is really really appreciated!

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();

        }
    };

You can create a hidden variable and increment it each time the user selects an answer. When you decide to display the result you can read the answers from this hidden variable.

If you need saved question forever, I mean when user exit from program and questions saved. In this case, you can save it in some table in database. If you need saved answer only with this user session, create some ArrayList and put in this list id of questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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