mercredi 26 juillet 2017

How do I generate random Strings for a multiple choice game

I am building a game that has a question with 4 multiple choice answers (in android studio). I am going to generate three wrong answers and 1 correct answer for all 45 questions in my app. I have a separate method that generates the question randomly (45 different questions). I am not quite sure how to do this.

public class MainActivity extends AppCompatActivity {

Button startGame;
TextView questionTextView;
questions question = new questions();
ArrayList<String> answers; //gets the correct
int correctAnswer;

public void START (View view) {

    startGame.setVisibility(View.INVISIBLE);

}

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

    Random rand = new Random();
    startGame = (Button) findViewById(R.id.startButton);
    questionTextView = (TextView) findViewById(R.id.questionTextview);
    questionTextView.setText(question.getQuestion());


    correctAnswer = rand.nextInt(4);

    for(int i = 0; i < 4; i++ ) {

        if(i == correctAnswer) {


        } else {


        }
    }
}

}

public class questions {

public String [] mquestions = {


        "Who is the 1st president of the United States?",
        "Who is the 2nd president of the United States?",
        "Who is the 3rd president of the United States?",
        "Who is the 4th president of the United States?",
        "Who is the 5th president of the United States?",
        "Who is the 6th president of the United States?",
        "Who is the 7th president of the United States?",
        "Who is the 8th president of the United States?",
        "Who is the 9th president of the United States?",
        "Who is the 10th president of the United States?",
        "Who is the 11th president of the United States?",
        "Who is the 12th president of the United States?",
        "Who is the 13th president of the United States?",
        "Who is the 14th president of the United States?",
        "Who is the 15th president of the United States?",
        "Who is the 16th president of the United States?",
        "Who is the 17th president of the United States?",
        "Who is the 18th president of the United States?",
        "Who is the 19th president of the United States?",
        "Who is the 20th president of the United States?",
        "Who is the 21st president of the United States?",
        "Who is the 22nd president of the United States?",
        "Who is the 23rd president of the United States?",
        "Who is the 24th president of the United States?",
        "Who is the 25th president of the United States?",
        "Who is the 26th president of the United States?",
        "Who is the 27th president of the United States?",
        "Who is the 28th president of the United States?",
        "Who is the 29th president of the United States?",
        "Who is the 30th president of the United States?",
        "Who is the 31st president of the United States?",
        "Who is the 32nd president of the United States?",
        "Who is the 33rd president of the United States?",
        "Who is the 34th president of the United States?",
        "Who is the 35th president of the United States?",
        "Who is the 36th president of the United States?",
        "Who is the 37th president of the United States?",
        "Who is the 38th president of the United States?",
        "Who is the 39th president of the United States?",
        "Who is the 40th president of the United States?",
        "Who is the 41st president of the United States?",
        "Who is the 42nd president of the United States?",
        "Who is the 43rd president of the United States?",
        "Who is the 44th president of the United States?",
        "Who is the 45th president of the United States?",

};


public String getQuestion() {

    String question = "";

    Random rand = new Random();
    //This randomizes the questions!!
    int randomNumber = rand.nextInt(mquestions.length);

    question = mquestions[randomNumber];

    return question;

}

}




Aucun commentaire:

Enregistrer un commentaire