2012-09-05 43 views
0

每當我點擊按鈕時,如果答案正確,它應該改變分數。但是,當我點擊下一個,它不會改變分數。它只顯示其默認值爲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)); 

} 
} 

回答

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

相關問題