0

我正在创建一个 Android 测验应用程序。我有这个程序的三个 .java 文件:TrueFalse.java、QuizActivity.java 和 Quiz.java,所有这些文件一起运行以运行应用程序。

我遇到的问题是我试图在测验完成后显示用户的分数。我的代码没有这样做。

这是我的 TrueFalse.java 代码:

package com.example.quiz;

public class TrueFalse {

public static final int NO_ANSWER = 1;
public static final int TRUE_ANSWER = 2;
public static final int FALSE_ANSWER = 3;

private int mQuestion;
private boolean mTrueQuestion;
private int mUserAnswer;

public TrueFalse(int question, boolean trueQuestion){
    mQuestion = question;
    mTrueQuestion = trueQuestion;
    mUserAnswer = NO_ANSWER;
}

public boolean getAnswerRight() {
    switch (mUserAnswer){
        case TRUE_ANSWER:
            return mTrueQuestion;
        case FALSE_ANSWER:
            return !mTrueQuestion;
    }
    return false;
}

public void setAnswer(boolean answer) {
    mUserAnswer = answer ? TRUE_ANSWER : FALSE_ANSWER; 
}

public boolean isQuestionAnswered(){
    return mUserAnswer != NO_ANSWER;
}

public int getQuestion(){
    return mQuestion;
}
}

这是我的 QuizActivity.java 代码:

    package com.example.quiz;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends Activity {

    private Button mTrueButton;
    private Button mFalseButton;
    private Button mPreButton;
    private Button mNextButton;
    private Button mQuitButton;
    private TextView mMessage;
    private Quiz mQuiz=new Quiz(new TrueFalse[]{
        new TrueFalse(R.string.message, true),
        new TrueFalse(R.string.message2, true),
        new TrueFalse(R.string.message3, true)
    });

    private int mCurrentQuestion = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);

        mTrueButton = (Button)findViewById(R.id.true_button);
        mFalseButton = (Button)findViewById(R.id.false_button);
        mMessage = (TextView)findViewById(R.id.message_box);
        mPreButton=(Button)findViewById(R.id.pre_button);
        mNextButton=(Button)findViewById(R.id.next_button);
        mQuitButton=(Button)findViewById(R.id.quit_button);

        mMessage.setText(mQuiz.getQuestions()[mCurrentQuestion].getQuestion());

        mTrueButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                showAnswer(true);
            }
        });

        mFalseButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                showAnswer(false);
            }
        });

        mPreButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                QuizActivity.this.mQuiz.preButton();
                updateButtonVisibility();
                QuizActivity.this.mMessage.setText(QuizActivity.this.mQuiz.getCurrentQuestion().getQuestion());
            }
        });

        mNextButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                QuizActivity.this.mQuiz.nextButton();
                updateButtonVisibility();
                QuizActivity.this.mMessage.setText(QuizActivity.this.mQuiz.getCurrentQuestion().getQuestion());
            }
        });
        mQuitButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                finish();
            }
        });
     }      

        public void updateButtonVisibility(){
            if (mQuiz.getCurrentQuestion().isQuestionAnswered()){
                mTrueButton.setVisibility(View.INVISIBLE);
                mFalseButton.setVisibility(View.INVISIBLE);
                }
                else{
                    mTrueButton.setVisibility(View.VISIBLE);
                    mFalseButton.setVisibility(View.VISIBLE);

                }

        }

        private void showAnswer(boolean userChoice){
            mQuiz.getCurrentQuestion().setAnswer(userChoice);
            Toast.makeText(this, mQuiz.getCurrentQuestion().getAnswerRight() ? R.string.toast_correct : R.string.toast_incorrect, Toast.LENGTH_LONG).show();
            updateButtonVisibility();

            if(mQuiz.areQuestionsLeft() == false){
            /*If I set the above line's false to true, I get a score right after
              the user answers the first question.*/
                mNextButton.setVisibility(View.INVISIBLE);
                mPreButton.setVisibility(View.INVISIBLE);
                mMessage.setText("Your score is: " + mQuiz.getScore());
            }
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.quiz, menu);
        return true;
    }

}

这是我的 Quiz.java 代码:

package com.example.quiz;

public class Quiz {
    private TrueFalse[] mQuestions;
    private int currentQuestion;

    public Quiz (TrueFalse[] questions){
        setQuestions(questions);
        currentQuestion = 0;
    }

    public TrueFalse[] getQuestions() {
        return mQuestions;
    }

    public void setQuestions(TrueFalse[] questions) {
        mQuestions = questions;
    }

    public boolean nextButton(){
        if (currentQuestion < mQuestions.length-1){
            currentQuestion++;
            return true;
        }
        return false;
    }

    public boolean preButton(){
        if (currentQuestion > 0){
            currentQuestion--;
            return true;
        }
        return false;
    }

    public TrueFalse getCurrentQuestion(){
        return mQuestions[currentQuestion];
    }

    public boolean areQuestionsLeft(){
        for (TrueFalse question: mQuestions)
        {
            if (question.isQuestionAnswered())
                return true;
        }
        return false;
    }

    public int getScore(){
        return getScore(0);
    }

    public int getScore(int bonus){
        for(TrueFalse question: mQuestions)
            bonus += question.getAnswerRight() ? 1 : 0;
        return bonus;
    }
}

在 QuizActivity.java 代码中,我有一个 showAnswer 函数,我在其中显示分数。这使用 Quiz.java 代码中的 areQuestionsLeft 函数来循环并发送一个布尔值,同样的函数使用 TrueFalse.java 代码中的 isQuestionAnswered 函数。

我可以看到的一件事是,在我的 Quiz.java 代码中,在 areQuestionsLeft 函数中,总是返回 for 循环内的值。

如果有人能指出我的错误在哪里,我将不胜感激!!!非常感谢!

4

1 回答 1

1

首先,我只是想称赞您的代码清洁度。我在 Stack 上相当多,并且不倾向于阅读大多数人懒惰尝试编码的可怕混乱,但我喜欢你的。

我找到了这个:

public boolean areQuestionsLeft(){
    for (TrueFalse question: mQuestions)
    {
        if (question.isQuestionAnswered())
            return true;
    }
    return false;
}

只要:

  1. mQuestions 中至少有 1 个问题
  2. 第一个问题已回答(真或假)

这是因为您实际上返回的是真实的,areQuestionsLeft()而不是计算已回答/未回答的问题的数量。

希望有帮助!

于 2013-10-04T02:19:53.267 回答