好吧,我有这个问题大约 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 个按钮之一时,应用程序停止