0

I am making a simple quiz app. How can I stop my questions from repeating after it has been answered? This is how I set my random questions

random = new Random() 

The questions are all in another activity called QuestionActivity. This is my Main 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);
}

This is My Question Activity code below

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;
}
}
Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
  • You can try to have a variable that holds the indices of those questions that were already answered, we can call this ```x``` and after every ```random = new Random();``` check if the randomize number is in ```x``` – Mark Melgo Feb 19 '19 at 07:22

1 Answers1

1

I would suggest a little bit of refactoring to make what you want to achieve simpler. Firstly, change your Question class to only contain the question, the choices for that question, the correct choice and whether or not the question has been answered (boolean). Secondly, create a QuestionBank class which contains a list of all Question object that you will use.

Here is some code for this

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

In your Activity get the instance of the QuestionBank class and fetch random questions from it. You can also add more methods and members to both the Question and QuestionBank class as per your needs.

Bilal Naeem
  • 962
  • 7
  • 13
  • One simple enhancement: a question doesn't care if it was asked before. The Bank does! In other words: simply have the bank **shuffle** that list, return the last element of the list, and then remove that! It is as simple as that: why ignore questions, when you can just take them completely out of the picture. – GhostCat Feb 19 '19 at 07:39
  • i don't understand can you please explain little bit more please – franklyn omeben Feb 19 '19 at 07:41
  • please i need a code example @ GhostCat – franklyn omeben Feb 19 '19 at 07:42