Skip to content Skip to sidebar Skip to footer

How To Randomize If Statements Within Android Development

I am building a questionnaire type application within android and I am wondering if there is a way to randomize the final output if the if statements have the same values? In other

Solution 1:

You could have a function to generate a random index from a string array of correct result responses.

String[] correctResults;
Random randomGenerator;

publicvoid onCreate (Bundle savedInstanceState) {
    //All your init stuff...generator = new Random();
    correctResults = newString[] {"good job", "well done"};
}

publicString getRandomCorrectResult() {
    return correctResults[generator.nextInt(correctResults.length)];
}

And just call getRandomCorrectResult() inside of that if statement!

Solution 2:

I'm not going to write actual code for you because that takes the fun away, but instead of displayResult("GOOD JOB") you could have a function called displayPositiveResult()

In this function you would randomly select a string from an array of strings containing things like "Great job!", "You did it!", "Way to go!"

You get the idea.

Hope this helps!

Post a Comment for "How To Randomize If Statements Within Android Development"