さて、私は約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;
}
問題はif
onClickメソッドの比較にあることを私は知っています。なぜなら、String buttonTextを他のものと比較すると、それが機能するからです。誰かが私がと比較できない理由を教えてくださいnextQuestion.correctAnswerText
。私は間違った場所で何かを宣言していますか?
観察:答え(正しいか間違っているか)を含む4つのボタンの1つをクリックすると、アプリが停止します