• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Calling Random Classes

chpsrmne

Lurker
Im currently working on an app that will work kind of like a quiz. What I am trying/hoping to accomplish is to have a random sound file played and have a couple of options to correctly match to the sound clip. The biggest problem I am having is calling the random sound file. I could get them to go in order but after a few times you could just memorize the answers. I was wondering if there was a way to randomly call a class or if there is a better way that I may not be thinking of. Kind of hit a wall and any help would be appreciated. Thanks in andvance

Chpsrmne
 
You could use Java's Random Class and use its nextInt👎 function, with a switch statement to choose the next sound file and questions.

Example code: (I didn't run this, should work though)

Code:
import java.util.Random;

public class YourClass
{
     private static final int MAX_NUMBER=5; //number of sounds
     private Random rnd= new Random(); //declaring random number generator and initializing it

     private void choose ()
     {
          //choose a random number from 0 to (1 below MAX_NUMBER)
          switch(rnd.nextInt(MAX_NUMBER))
          {
                case 0:
                          // if 1st sound is picked, do stuff
                         break;
                case 1:
                          // if 2nd sound is picked, do stuff
                         break;
                case 2:
                          // if 3rd sound is picked, do stuff
                         break;
                case 3:
                          // if 4th sound is picked, do stuff
                         break;
                case 4:
                          // if 5th sound is picked, do stuff
                         break;
                //keep going until you have a case for each sound
                //in this case with MAX_NUMBER at 5, I would stop at 4
          }
     }
}

Just add in the declaration and a similar method into the class you're using and that should work
 
Back
Top Bottom