无法使用if语句计算quiz app android的正确答案

时间:2012-09-05 05:24:03

标签: android if-statement count

每当我点击按钮时,如果答案正确,它应该改变分数。但是,当我点击下一步时,它不会改变分数。它只显示默认值为0.除了得分,一切都没问题。

 public class QuizActivity extends Activity {
    DBAdapter db = new DBAdapter(this);
    private TextView tvQuestion;
    private Cursor c;
    private Cursor cur;
    private RadioGroup radioGroup;
    private RadioButton rb0,rb1,rb2;
    private Button nextButton;
    private int i =1;
    private int questNo=0;
    private RadioButton radioButton;
    private int score=0;
    private String correctAnswer;

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


    displayQuestion();
    nextButton = (Button) findViewById(R.id.nextButton);
    nextButton.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            getselectedAnswer();
            checkAnswer();

        if(i < 10){
                i=i+1;


                radioGroup.clearCheck();
                displayQuestion();

                }else{
                Intent i = new Intent(QuizActivity.this, Results.class);
                startActivity(i);
                }
        }
        });
}
        public void checkAnswer() {
        Cursor mCursor = db.getCorrectAnswersforComparison(i);
        String correctAns = mCursor.getString(mCursor.getColumnIndex(DBAdapter.KEY_CORRECTANSWERS));
        if(getselectedAnswer()==correctAns){
            score = score+1;

        }else{
            score = score+0;
        }

        }

private String getselectedAnswer() {    
    if (rb1.isChecked()){
        return rb1.getText().toString();
    }
    if (rb2.isChecked()){
        return rb2.getText().toString();
    }   
    if (rb0.isChecked()){
        return rb0.getText().toString();
    }
    else
        return null;


}
public void displayQuestion(){
    db.open();
    TextView scoretv = (TextView) findViewById(R.id.scoretv);
    scoretv.setText(""+score);
    c = db.getQuiz_Content(i);
    cur = db.getAnswers(i);
    tvQuestion = (TextView) findViewById(R.id.textViewQuestionQA);
    tvQuestion.setText(c.getString(0));
    radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
    String correctAnswer = cur.getString(cur.getColumnIndex(DBAdapter.KEY_CORRECTANSWERS));
    String option1 = cur.getString(cur.getColumnIndex(DBAdapter.KEY_OPTION1));
    String option2 = cur.getString(cur.getColumnIndex(DBAdapter.KEY_OPTION2));
    rb0 = (RadioButton) findViewById(R.id.radio0);
    rb1 = (RadioButton) findViewById(R.id.radio1);
    rb2 = (RadioButton) findViewById(R.id.radio2);
    List<String> answers = new ArrayList<String>();
    answers.add(correctAnswer);
    answers.add(option1);
    answers.add(option2);
    Collections.shuffle(answers);
    rb0.setText(answers.get(0));
    rb1.setText(answers.get(1));
    rb2.setText(answers.get(2));

}
}

1 个答案:

答案 0 :(得分:2)

你的问题在这里:

if(getselectedAnswer()==correctAns)

Java上的字符串比较不适用于通常的相等运算符==(这会检查它们是否是同一个实例,而不是字符串是否包含相同的字符)。相反,使用字符串方法.equals:

if(getSelectedAnswer().equals(correctAns))