挑战练习--评分(3.8)(Android权威编程指南)

要评分,就要对回答正确的次数和答题的次数进行统计,当全部完成之后toast分数,
下面是部分源代码
* CorrectAnswer用于记录回答正确的次数
answerLength是回答问题的总个数

    private double CorrectAnswer;
    private double answerLength=0;//回答问题的个数
  • 在checkAnswer方法中,判断回答正确之后, CorrectAnswer加一
private void checkAnswer(boolean userResult){
        boolean questionresult=mQuestions[mCurrentIndex].isAnswerTrue();
        int textResId=0;

        if(userResult==questionresult){
            mQuestions[mCurrentIndex].setIsAnswerd(1);
            textResId=R.string.correct_toast;
            //次数加一
            CorrectAnswer++;
        }else{
            mQuestions[mCurrentIndex].setIsAnswerd(0);
            textResId=R.string.incorrect_toast;
        }
        ButtonEnabled();
        Toast.makeText(this, textResId, Toast.LENGTH_SHORT).show();
    }
  • 在next之后,对回答题目的次数加一,并且当全部回答后,toast分数
next_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentIndex=(mCurrentIndex+1)%mQuestions.length;
                update();
                //显示分数
                answerLength++;
                if(answerLength==mQuestions.length){
                    double i=CorrectAnswer/mQuestions.length;
                    double score=i*100;
                    Toast.makeText(QuizeActivity.this, "score="+score, Toast.LENGTH_SHORT).show();
                }

            }
        });

猜你喜欢

转载自blog.csdn.net/ayangann915/article/details/81147337
今日推荐