Java 如何防止ramdom问题重复

Java 如何防止ramdom问题重复,java,android,Java,Android,我正在制作一个简单的测验应用程序。我怎样才能阻止我的问题在被回答后重复?这就是我设置随机问题的方式 random = new Random() 这些问题都在另一个名为QuestionActivity的活动中。 这是我的主要活动: private Question question = new Question(); private String answer; private int questionLength = question.questions.length; Random r

我正在制作一个简单的测验应用程序。我怎样才能阻止我的问题在被回答后重复?这就是我设置随机问题的方式

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

我建议进行一点重构,使您想要实现的目标更简单。首先,将您的问题类更改为仅包含问题、该问题的选项、正确的选项以及是否已回答该问题(布尔值)。其次,创建一个QuestionBank类,其中包含您将使用的所有问题对象的列表

下面是一些代码

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()));
    }
}
课堂提问{
字符串问题;
字符串[]选项;
int校正选项;
布尔解;
//根据需要创建构造函数和getter setter
}
班级问题库{
列出问题;
//创建构造函数或使其成为单例
问题({
列出未回答的问题=ArrayList();
(问题:问题){
如果(!question.isAnswered){未回答的问题。添加(问题);}
}
随机=新随机();
返回未回答的问题.get(random.nextInt(unansweredQuestions.size());
}
}

在您的活动中,获取QuestionBank类的实例并从中获取随机问题。您还可以根据需要向Question和QuestionBank类添加更多方法和成员。

您可以尝试使用一个变量来保存已回答问题的索引,我们可以将其称为
x
,并在每次
random=new random()之后使用
检查随机化数字是否在
x
一个简单的增强:一个问题不在乎以前是否被问过。银行有!换句话说:只需让银行洗牌该列表,返回列表的最后一个元素,然后删除它!这很简单:既然你可以把问题完全从图片中抹去,为什么要忽略这些问题呢?我不明白,请你再解释一下好吗?请给我一个代码示例@GhostCat