vendredi 1 juin 2018

Getting random questions in an Array without repeating questions

im pretty new in programming and I'm trying to generate random questions into a quiz. I could manage that the questions are random, but the questions are still repeating. How can i keep them from duplicating? I've alredy seen several articles but I just don't quite understand how to implement the code.

This is my main Activity where the questions are stored and shown:

public class MainActivity extends AppCompatActivity {
private TextView textView;
private Button generateQbutton;
private boolean aBoolean;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView) findViewById(R.id.textV);
    generateQbutton = (Button) findViewById(R.id.buttonP);

    randomFacts ();

    generateQbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder altdial = new AlertDialog.Builder(MainActivity.this);
            altdial.setMessage(factArray[0].getmDialog()).setPositiveButton("k", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            AlertDialog ctnalert =  altdial.create();
            ctnalert.show();

            randomFacts ();

        }
    });

}
public void randomFacts (){
    shuffelFacts ();
    textView.setText(factArray[0].getmFacts());
    aBoolean = factArray[0].ismAnswers();

}
Facts f01 = new Facts(true, "Quesstion1?", "Dialog for Q1!");
Facts f02 = new Facts(false, "Quesstion2??", "Dialog for Q2!");
Facts f03 = new Facts(true, "Quesstion3??", "Dialog for Q3!");
Facts f04 = new Facts(false, "Quesstion4??", "Dialog for Q4!");
Facts f05 = new Facts(true, "Quesstion5?", "Dialog for Q5!");


Facts [] factArray = new Facts[]{
        f01,f02,f03,f04,f05,
};
public void shuffelFacts(){
    Collections.shuffle(Arrays.asList(factArray));
}

}

This is my constructor:

public class Facts {
private boolean mAnswers;
private String mFacts;
private String mDialog;

public Facts(boolean mAnswers, String mFacts, String mDialog) {
    this.mAnswers = mAnswers;
    this.mDialog = mDialog;
    this.mFacts = mFacts;
}

public boolean ismAnswers() {
    return mAnswers;
}

public String getmFacts() {
    return mFacts;
}
public String getmDialog(){
    return mDialog;
}

}




Aucun commentaire:

Enregistrer un commentaire