2013-03-08 3 views
0

글쎄,이 문제는 약 3 일입니다. 퀴즈 게임을 만들고 있지만 클릭 한 버튼에 정답이 포함되어 있는지 여부를 확인할 수 없습니다. 여기에서 보았던 몇 가지 팁을 따라 제 코드를 수정했습니다. 새로운 코드를 참조하십시오 : 나는이 질문 클래스 코드에서 객체를 생성하고 static 객체로 작업하지만, 팁을 다음했다Android : 퀴즈 게임에서 정답을 식별 할 수 없습니다.

package com.app; 

import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 
import java.util.Random; 

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

public class MyActivity extends Activity implements OnClickListener { 


    TextView textView1, textView2; 
    Button btn1, btn2, btn3, btn4; 

    ArrayList<Question> qsts = new ArrayList<Question>(); 
    List<Integer> generated = new ArrayList<Integer>(); 

    ArrayList<String> allAnswers = new ArrayList<String>(); 

    Random rng = new Random(); 
    Question nextQuestion; 


// Creating the objects Question that recieves the values "questionText", "correctAnswerText", and the other 3 wrong answers "wrongAnswer1" 2 and 3... 

    Question q1 = new Question(
      "Question 1", 

      "Correct Answer - Question 1", 
      "Wrong 1 - Question 1", 
      "Wrong 2 - Question 1", 
      "Wrong 3 - Question 1" 
      ); 
    Question q2 = new Question(
      "Question 2", 

      "Correct Answer - Question 2", 
      "Wrong 1 - Question 2", 
      "Wrong 2 - Question 2", 
      "Wrong 3 - Question 2" 
      ); 
    Question q3 = new Question(
      "Question 3", 

      "Correct Answer - Question 3", 
      "Wrong 1 - Question 3", 
      "Wrong 2 - Question 3", 
      "Wrong 3 - Question 3" 
      ); 

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


    // ADD THE QUESTIONS IN THE ArrayList qsts 

    qsts.add(q1);   
     qsts.add(q2); 
     qsts.add(q3); 

     textView1 = (TextView) findViewById(R.id.textView1); 
      textView2 = (TextView) findViewById(R.id.textView2); 

      btn1 = (Button) findViewById(R.id.btn1); 
      btn2 = (Button) findViewById(R.id.btn2); 
      btn3 = (Button) findViewById(R.id.btn3); 
      btn4 = (Button) findViewById(R.id.btn4); 

      btn1.setOnClickListener(this); 
      btn2.setOnClickListener(this); 
      btn3.setOnClickListener(this); 
      btn4.setOnClickListener(this); 

     generateQuestion(); 

    } 

     public void generateQuestion(){ 




      while(true){ 

       int nxt = rng.nextInt(3); 

       if (!generated.contains(nxt)){ 

        generated.add(nxt); 

        Question nextQuestion = qsts.get(nxt); 

        textView1.setText(nextQuestion.questionText); 

        allAnswers.add(nextQuestion.correctAnswerText); 
        allAnswers.add(nextQuestion.wrongAnswer1); 
        allAnswers.add(nextQuestion.wrongAnswer2); 
        allAnswers.add(nextQuestion.wrongAnswer3); 

        Collections.shuffle(allAnswers); 

        btn1.setText(allAnswers.get(0)); 
        btn2.setText(allAnswers.get(1)); 
        btn3.setText(allAnswers.get(2)); 
        btn4.setText(allAnswers.get(3)); 

        break; 
       } 
      } 
     } 

     @Override 
     public void onClick(View v) { 
      Button b = (Button)v; 
      String buttonText = b.getText().toString(); 

      if(buttonText.equals(nextQuestion.correctAnswerText)) 
      { 

       textView2.setText("CORRECT!"); 
       generateQuestion(); 
       return; 

      }else{ 
       textView2.setText("WRONG!"); 
       generateQuestion(); 
       return; 
      } 

     } 
} 

을, 그 방법을 만들었다. 여기

클래스 코드 :

package com.app; 

public class Question { 

    String questionText; 
    String correctAnswerText;  
    String wrongAnswer1; 
    String wrongAnswer2; 
    String wrongAnswer3; 


    Question (String qst, String cAns, String wAns1, String wAns2, String wAns3){ 

     questionText = qst; 
     correctAnswerText = cAns; 
     wrongAnswer1 = wAns1; 
     wrongAnswer2 = wAns2; 
     wrongAnswer3 = wAns3; 

    } 

내가 뭔가 다른 문자열 - 버튼을 비교할 때 문제가 온 클릭 방법, '원인에 if의 comparation에 알고, 그것을 작동합니다. 제발 누군가가 왜 내가 nextQuestion.correctAnswerText과 비교할 수 없는지 말해주세요. 나는 잘못된 장소에서 뭔가를 선언하고 있는가?

관측 : 나는 (올바른 또는 잘못된) 대답을 포함 4 개 개의 버튼 중 하나를 클릭하면 응용 프로그램이 정지

+0

'관찰 : 앱이 멈 춥니 다'앱이란 무엇을 의미합니까? 그것은 추락합니까? – Simon

+0

정확한 코드입니까? –

+0

그래, @ 사이먼. 그것은 충돌합니다. 죄송합니다. 제 첫 언어가 아닙니다. –

답변

1

이 두 번 Question nextQuestion를 선언하는 참조하십시오. 하나는 전역으로, 다른 하나는 기능 있음 generateQuestion()

그리고 nextQuestion은 null이며 이는 귀하의 잘못입니다.

는 귀하의 코멘트에 대한 nextQuestion = qsts.get(nxt);

솔루션과 기능 generateQuestion()이 (Question nextQuestion = qsts.get(nxt);) 라인을 교체합니다. allAnswers.clear() 새 질문을 생성하기 전에 수행하십시오.

 if(buttonText.equals(nextQuestion.correctAnswerText)) 
     { 

      textView2.setText("CORRECT!"); 
      allAnswers.clear(); 
      generateQuestion(); 
      return; 

     }else{ 
      textView2.setText("WRONG!"); 
      allAnswers.clear(); 
      generateQuestion(); 
      return; 
     } 
+0

글로벌? 이 사이트에는 없습니다 ... – Simon

+0

@ 사이먼은 잘못되었습니다 –

+0

http://c2.com/cgi/wiki?GlobalVariablesAreBad. 가장 중요한 설계 원칙은 모든 변수가 가능한 가장 짧은 수명을 가져야한다는 것입니다. 이 경우에는 클래스 필드가 적합하지만 전역 적이는 것은 아닙니다. – Simon