I'm trying to use Math.random to pick a random number, then use that number as an index for a String array, but I keep getting "ArrayIndexOutOfBoundsException" error, even when the index is within the bounds of the array. Why is this? Here's the code that it says is causing the problem.
public static int enemyPicker = 0;
public static String[] enemies = {"skeleton, goblin, orc, troll, zombie, ogre, slimes, lions, giant scorpion, spider"};
public static void main(String[] args) {
enemyPicker = (int) Math.ceil(Math.random() *9);
System.out.println("A " + enemies[enemyPicker] + " appeared!");
}
Aucun commentaire:
Enregistrer un commentaire