Android:在测验游戏中无法识别正确的答案

时间:2013-03-08 18:28:40

标签: android multiple-choice

好吧,我这个问题大概3天了。我正在进行一个问答游戏,但我无法确定点击的按钮是否包含正确的答案。根据我在这里看到的一些提示,我在代码中做了一些改动。请参阅新代码:

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

        }
}

我在Question类代码中创建了对象,并且正在使用static个对象,但是按照提示,我就这样做了。

这是类代码:

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;

    }

我知道问题出在onClick方法中if的比较中,因为当我将String buttonText与其他东西进行比较时,它会起作用。请有人告诉我为什么我无法与nextQuestion.correctAnswerText进行比较。我在错误的地方宣布了什么吗?

观察:当我点击包含答案(正确或错误)的4个按钮之一时,应用程序停止

1 个答案:

答案 0 :(得分:1)

看到您宣布Question nextQuestion两次。 一个是全球性的,另一个是函数generateQuestion()

全局nextQuestion为空,这是您的错误。

Question nextQuestion = qsts.get(nxt);函数generateQuestion()替换为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;
        }