mardi 22 décembre 2020

Android Studio Java - How do I use a random array without repeating?

I've got an array of random messages, but I want it so that it doesn't pick a message that has already been picked and then reset once all messages have been picked.

public void showRandomMsg(){
        shuffleMsg();
        answer1.setText((messageArray[0].getmAns()));
        message2.setText((messageArray[0].getmMsg()));
        toyView1.setImageResource(messageArray[0].getmImage());
    }

Messages m01 = new Messages(R.drawable.crown1, "Mesage 0 A","Message 0 B");
Messages m02 = new Messages(R.drawable.crown2,"Mesage 1 A","Message 1 B");
Messages m03 = new Messages(R.drawable.crown3,"Mesage 2 A","Message 2 B");
Messages m04 = new Messages(R.drawable.crown4,"Mesage 3 A","Message 3 B");
Messages m05 = new Messages(R.drawable.crown5,"Mesage 4 A","Message 4 B");

Messages [] messageArray=new Messages[]{
        m01, m02, m03, m04, m05
};

public void shuffleMsg(){
    Collections.shuffle(Arrays.asList(messageArray));

}



Aucun commentaire:

Enregistrer un commentaire