0

好吧,我是 Java 编程的新手,按照我在这里看到的一些技巧,我将这段代码制作成了一个问答游戏:

public class OtherActivity extends Activity {


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

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

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


        final ArrayList<Question> qsts = new ArrayList<Question>();
        qsts.add(Question.q1);
        qsts.add(Question.q2);
        qsts.add(Question.q3);
        qsts.add(Question.q4);
        qsts.add(Question.q5);
        qsts.add(Question.q6);

        final Random rng = new Random();

        final List<Integer> generated = new ArrayList<Integer>();
        int nxt = rng.nextInt(6);

                    generated.add(nxt);

                    final Question nextQuestion = qsts.get(nxt);

                    textView1.setText(nextQuestion.questionText);

                    final ArrayList<String> allAnswers = new ArrayList<String>();
                    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));

                    btn1.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {
                            if(allAnswers.get(0) == nextQuestion.correctAnswerText){
                                textView2.setText("CORRECT ANSWER, MAN!");

                                while(true){

                                    int nxt = rng.nextInt(6);

                                    if (!generated.contains(nxt)){

                                        generated.add(nxt);

                            Question nextQuestion = qsts.get(nxt);

                            textView1.setText(nextQuestion.questionText);

                            ArrayList<String> allAnswers = new ArrayList<String>();
                            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;
                                    }
                                }

                            }
                            else{
                                textView2.setText("WRONG ANSWER, MAN!");

                                while(true){

                                    int nxt = rng.nextInt(6);

                                    if (!generated.contains(nxt)){

                                        generated.add(nxt);

                            //  ---->   Integer nxt = rng.nextInt(6); <---- random nxt aqui!

                            Question nextQuestion = qsts.get(nxt);

                            textView1.setText(nextQuestion.questionText);

                            ArrayList<String> allAnswers = new ArrayList<String>();
                            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;
                                    }
                                }

                            }
                        }
                    });

                    //
                    // AND THE SAME METHOD TO THE BUTTONS btn2, btn3, btn4
                    //
                    // btn2 with a if (allAnswers1.get(1) == nextQuestion.correctAnswerText) { ...
                    // btn3 with a if (allAnswers1.get(2) == nextQuestion.correctAnswerText) { ...
                    // btn4 with a if (allAnswers1.get(3) == nextQuestion.correctAnswerText) { ...
                    //

    }
}

我有这个其他类代码:

    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;

    }

    static Question q1 = new Question(
            "Question 1",

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

            "Correct answer - question 2",
            "Wrong Answer 1 - question 2",
            "Wrong Answer 2 - question 2",
            "Wrong Answer 3 - question 2"
            );
    // ...
    // and the same with q3, q4, q5 and q6
    // ...

好吧,你怎么看,我在识别正确答案时遇到了问题。在第一个问题中它工作正常,但在此之后就不行了。我相信这个问题会发生,因为当我尝试比较 allAnswers.get(0) == nextQuestion.correctAnswerText或与 ArrayList allAnswers 的 (1)、(2) 或 (3) 等其他索引进行比较时,为比较选择的“槽”仍然具有第一个问题文本答案。所以我该怎么做?

*Plus:代码变得非常大,我尝试if在 onClickListener 中放入一个并与id4 个按钮中的一个进行比较,但它必须与 onCreate 之外的其他函数一起使用,因为 ArrayList qsts(包含问题objects) 未声明,我无法比较单击的按钮,因为我必须声明一个不同的 ArrayList。有更好的方法来做我想做的事吗?

4

4 回答 4

0

For problem of having large code i suggest u to make separate onclicklistener and put that button.setonclicklistener in all 4 buttons.

Then on click listener you got view as parameter, that view is a button that is pressed. cast it to button and then get text of button. then u can set if else leader that you implement in button action.

Make different method to set new question so that you have to just call it rather then writing same code multi times.

I hope it will resolve both of your problems.

于 2013-03-08T04:15:26.340 回答
0

遵循维克兰特所说的指示。

此外,请放置一个布尔变量,当用户选择任何选项时,该变量的值将发生变化。

如何通过检查该布尔值来获取用户是否已回答。

要检查正确的答案,您必须将所选值与正确值进行比较。

享受编码。:)

于 2013-03-08T04:18:33.350 回答
0

听是维克兰特所说的例子

public class MyActivity extends Activity implements OnClickListener {

Question nextQuestion;

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

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

   if(buttonText.equals( nextQuestion.correctAnswerText ))
   { 
      DoSomething();
      return;
   }else{

   }
}

不要忘记添加

Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(this);
于 2013-03-08T04:32:43.227 回答
0

如果您想创建测验应用程序,请检查此链接

还检查此链接以使用现有数据库与 Android 应用程序

如果您对此有任何问题,请告诉我。

祝你好运。

于 2013-03-08T05:48:57.677 回答