Android编程:测验问题不起作用

时间:2013-10-04 01:27:22

标签: java android

我正在创建一个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循环内的值总是返回。

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

1 个答案:

答案 0 :(得分:1)

首先,我只是想赞扬你的代码清洁度。我在Stack上玩得很开心,而且不会读大多数人对编码的懒惰尝试,但我很高兴。

我发现了这个:

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

只要符合以下条件,每次都会返回true:

  1. mQuestions中至少有一个问题
  2. 第一个问题已经回答(无论是真还是假)
  3. 这是因为您实际上areQuestionsLeft() 返回而非计算已经/避免的问题数量已经回答了。

    希望有所帮助!