java - 如何阻止随机问题重复
问题描述
我正在制作一个简单的测验应用程序。回答后如何阻止我的问题重复?这就是我设置随机问题的方式
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;
}
}
解决方案
我建议进行一些重构,以使您想要实现的目标更简单。首先,将您的 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 类添加更多方法和成员。
推荐阅读
- printing - Cypher - 尝试将所有节点打印到文本 o/p 超出 Java 堆空间 - Neo4j V 3.5
- intellij-idea - 如何在maven中用EAR、EJB和WEB构建JEE项目?
- regex - 使用 ant 语句 replaceregexp 时出现错误“无法重命名临时文件”
- react-native - React Native 在 react-native run-ios 上崩溃
- odoo - 如何通过 XML RPC 在 Odoo 11 中为发票注册付款
- amazon-dynamodb - AWS rekognition 可以检测图像中的表并允许我摄取到 dynamoDB 表中吗
- java - Java中有没有隐式使用反射?
- r - 如何从 R 关闭浏览器窗口?
- java - 如何在 Android 的 RelativeLayout 中使用 ScrollView?
- ng-bootstrap - 获取对嵌套在模式中的元素的引用