jeudi 3 mars 2016

generating random string from array then remove it so it wont regenerate

    TextView textView1 = (TextView) findViewById(R.id.textView1);
    int randomArray = (int) (Math.random() *4);
    textView1.setText(questions[randomArray][0]);

Using the code above (using android studio to develop an app) I can generate a random question from my array as shown below.

 String[][] question= new String[20][5];

 {
     question[0][0] = "what is my name?";
     question[0][1] = "Mark";
     question[0][2] = "Steven";
     question[0][3] = "Alan";
     question[0][4] = "Bob";

     question[1][0] = "what is my age?";
     question[1][1] = "30";
     question[1][2] = "31";
     question[1][3] = "32";
     question[1][4] = "33";
 }

The code below shows how I allocate the answers to the buttons so that they randomize to different buttons each time:

    Button button2 = (Button)findViewById(R.id.button2);
    int randomArray1 = (int) (Math.random() *4);
    button2.setText(questions[randomArray][randomArray1+1]);

    Button button3 = (Button)findViewById(R.id.button3);
    int randomArray2 = (int) (Math.random() *4);
    button3.setText(questions[randomArray][randomArray2+1]);

    Button button4 = (Button)findViewById(R.id.button4);
    int randomArray3 = (int) (Math.random() *4);
    button4.setText(questions[randomArray][randomArray3+1]);

    Button button5 = (Button)findViewById(R.id.button5);
    int randomArray4 = (int) (Math.random() *4);
    button5.setText(questions[randomArray][randomArray4+1]);

The +1 just means the question will not be placed in the buttons. All I want help with is how I would go about removing the random selection from the options so that I dont have a question that has 2,3 or 4 buttons saying steve or 30 etc.




Aucun commentaire:

Enregistrer un commentaire