我正在制作一个简单的测验应用程序。在回答问题后,如何停止重复我的问题?这就是我设置随机问题的方式
random = new Random()
问题全部在另一个名为QuestionActivity
的活动中。
这是我的主要活动:
private Question question = new Question();
private String answer;
private int questionLength = question.questions.length;
Random random;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
random = new Random();
btn_one = (Button)findViewById(R.id.btn_one);
btn_one.setOnClickListener(this);
btn_two = (Button)findViewById(R.id.btn_two);
btn_two.setOnClickListener(this);
btn_three = (Button)findViewById(R.id.btn_three);
btn_three.setOnClickListener(this);
btn_four = (Button)findViewById(R.id.btn_four);
btn_four.setOnClickListener(this);
tv_question = (TextView)findViewById(R.id.tv_question);
NextQuestion(random.nextInt(questionLength));
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_one:
if(btn_one.getText() == answer){
Toast.makeText(MainActivity.this, "You Are Correct", Toast.LENGTH_SHORT).show();
NextQuestion(random.nextInt(questionLength));
}else{
GameOver();
}
break;
case R.id.btn_two:
if(btn_two.getText() == answer){
Toast.makeText(MainActivity.this, "You Are Correct", Toast.LENGTH_SHORT).show();
NextQuestion(random.nextInt(questionLength));
}else{
GameOver();
}
break;
case R.id.btn_three:
if(btn_three.getText() == answer){
Toast.makeText(MainActivity.this, "You Are Correct", Toast.LENGTH_SHORT).show();
NextQuestion(random.nextInt(questionLength));
}else{
GameOver();
}
break;
case R.id.btn_four:
if(btn_four.getText() == answer){
Toast.makeText(MainActivity.this, "You Are Correct", Toast.LENGTH_SHORT).show();
NextQuestion(random.nextInt(questionLength));
}else{
GameOver();
}
break;
}
}
private void GameOver(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder
.setMessage("Game Over")
.setCancelable(false)
.setPositiveButton("New Game", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
})
.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
}
});
alertDialogBuilder.show();
}
private void NextQuestion(int num){
tv_question.setText(question.getQuestion(num));
btn_one.setText(question.getchoice1(num));
btn_two.setText(question.getchoice2(num));
btn_three.setText(question.getchoice3(num));
btn_four.setText(question.getchoice4(num));
answer = question.getCorrectAnswer(num);
}
这是下面的“我的问题”活动代码
public class Question {
public String questions[] = {
"Which is a Programming Language?",
"In COMAL language program, after name of procedure parameters must be in?",
"Programming language COBOL works best for use in?"
};
public String choices[][] = {
{"HTML", "CSS", "Vala", "PHP"},
{"Punction Marks", "Back-Slash", "Brackets", "Semi Colon"},
{"Siemens Applications", "Student Applications", "Social Applications", "Commercial Applications"}
};
public String correctAnswer[] = {
"PHP",
"Brackets",
"Commercial Applications"
};
public String getQuestion(int a){
String question = questions[a];
return question;
}
public String getchoice1(int a){
String choice = choices[a][0];
return choice;
}
public String getchoice2(int a){
String choice = choices[a][1];
return choice;
}
public String getchoice3(int a){
String choice = choices[a][2];
return choice;
}
public String getchoice4(int a){
String choice = choices[a][3];
return choice;
}
public String getCorrectAnswer(int a){
String answer = correctAnswer[a];
return answer;
}
}
答案 0 :(得分:1)
我建议您进行一些重构,以使您想要实现的目标变得更简单。首先,将您的Question类更改为仅包含问题,该问题的选择,正确的选择以及是否已回答该问题(布尔值)。其次,创建一个QuestionBank类,其中包含将要使用的所有Question对象的列表。
这是一些代码
class Question {
String question;
String[] options;
int correctOption;
boolean isAnswered;
//Create constructor and getter setter as per needs
}
class QuestionBank {
List<Question> questions;
//Create constructor or make it singleton
Question getRandomQuestion() {
List<Question> unansweredQuestions = ArrayList();
for(Question question: questions) {
if (!question.isAnswered) { unansweredQuestions.add(question); }
}
Random random = new Random();
return unansweredQuestions.get(random.nextInt(unansweredQuestions.size()));
}
}
在您的Activity中,获取QuestionBank类的实例,并从中获取随机问题。您还可以根据需要向Question和QuestionBank类添加更多方法和成员。