gpt4 book ai didi

java - 如何防止随机问题重复

转载 作者:行者123 更新时间:2023-12-02 10:14:48 24 4
gpt4 key购买 nike

我正在制作一个简单的测验应用程序。在回答完我的问题后,如何防止问题再次出现?这就是我设置随机问题的方式

random = new Random() 

这些问题都在另一个名为 QuestionActivity 的 Activity 中。这是我的主要 Activity :

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

这是下面的我的问题 Activity 代码

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 类更改为仅包含问题、该问题的选项、正确的选项以及问题是否已得到解答( boolean 值)。其次,创建一个 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 类添加更多方法和成员。

关于java - 如何防止随机问题重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54760680/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com