Originally Posted by
efluvio
...i think i should use the switch...
I'm liking that idea. You could use an array of ints to determine the order in which the things are executed.
A very simple approach might be something like:
static void foo(int [] order) {
for (int selection = 0; selection < order.length; selection++) {
switch (order[selection]) {
case 0:
System.out.println("Executing first thingie.");
// Do the first one
break;
case 1:
System.out.println("Executing second thingie.");
// Do the second one
break;
case 2:
System.out.println("Executing third thingie.");
// Do the third one
break;
case 3:
System.out.println("Executing fourth thingie.");
// Do the fourth one
break;
case 4:
System.out.println("Executing fifth thingie.");
// Do the fifth one
break;
} // End switch.
} // End for
} // End foo;
Start out with the array in any order you want, then, from the calling function, re-arrange the elements of the array each time before calling:
public class Z {
// Use rng.nextInt() in the randomShuffle method.
static Random rng = new Random();
static Scanner keyboard = new Scanner(System.in);
public static void main(String [] args) {
// Start with 0..4 in order
int orderArray[] = {0, 1, 2, 3, 4};
while (true) {
// Re-arrange the order in which things will be
// executed each time.
randomShuffle(orderArray);
foo(orderArray);
System.out.print("\nPress 'Enter' to continue...");
keyboard.nextLine();
System.out.println();
}
} // End main
static void foo(int [] order) {
//
// Code to do stuff in sequence, depending on the order.
//
} // End foo;
public static void randomShuffle(int [] iarray) {
// Uncomment the following for debugging
//System.out.printf("Into shuffle: %s\n", Arrays.toString(iarray));
//
// Code to shuffle the elements if iarray
//
// Uncomment the following for debugging
//System.out.printf("Out of shuffle: %s\n", Arrays.toString(iarray));
} // End shuffle
} // End class definition.
Some folks might prefer to put the shuffling inside foo so that the mechanism would be invisible to the calling function. That might be a better implementation, but the idea is the same.
Random shuffle algorithms can be very simple and easy to implement in Java. For example there is some pseudo-code for the
Fisher-Yates shuffle that can be efficiently implemented in something like seven or so lines of elementary Java.
Anyhow...
A sample run might look like:
Executing fourth thingie.
Executing third thingie.
Executing first thingie.
Executing fifth thingie.
Executing second thingie.
Press 'Enter' to continue...
Executing first thingie.
Executing fourth thingie.
Executing second thingie.
Executing third thingie.
Executing fifth thingie.
Press 'Enter' to continue...
Executing fifth thingie.
Executing first thingie.
Executing third thingie.
Executing second thingie.
Executing fourth thingie.
Press 'Enter' to continue...
Executing second thingie.
Executing third thingie.
Executing first thingie.
Executing fourth thingie.
Executing fifth thingie.
Press 'Enter' to continue...
Executing fourth thingie.
Executing first thingie.
Executing third thingie.
Executing second thingie.
Executing fifth thingie.
Press 'Enter' to continue...
Executing second thingie.
Executing fourth thingie.
Executing fifth thingie.
Executing first thingie.
Executing third thingie.
Press 'Enter' to continue...
Executing second thingie.
Executing fifth thingie.
Executing first thingie.
Executing third thingie.
Executing fourth thingie.
Press 'Enter' to continue...
Executing third thingie.
Executing first thingie.
Executing fourth thingie.
Executing second thingie.
Executing fifth thingie.
Press 'Enter' to continue...
Executing first thingie.
Executing fifth ...
Finally: I think that learning to write shuffling code is very worthwhile, but actually you can use the
Collections.shuffle() method on a list that is based on an array of Integers. I think that's also worth a glimpse.
Cheers!
Z