无法为测验应用程序android使用if语句计算正确答案

无法为测验应用程序android使用if语句计算正确答案

问题描述:

每当我点击按钮时,如果答案正确,它应该改变分数。但是,当我点击下一个,它不会改变分数。它只显示其默认值为0.除了分数,一切都可以。无法为测验应用程序android使用if语句计算正确答案

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

} 
} 

你的问题是在这里:关于Java

if(getselectedAnswer()==correctAns) 

字符串比较不常用的平等的==操作符的工作(此检查,如果他们是相同的实例,而不是是否字符串包含相同的字符)。相反,使用字符串方法.equals:

if(getSelectedAnswer().equals(correctAns)) 
+0

对不起。我看到我错过了什么。我试图从correctanswerstable得到的rowindex是不正确的。它在checkanswer方法之前从i中减去1。我将它改为Cursor mCursor = db.getCorrectAnswersforComparison(i-1);然后使用if(getSelectedAnswer()。equals(correctAns)) 。现在它可以工作......感谢一下! – omi0301