I'm relatively new to programming in Java.
I've got a task for class in which I have to populate an array with random numbers from 0 to 100. However the user needs to be able to define the number of random numbers which are inserted within this array. For this I've created the variable 'n' for now.
I think I've got most of the code nailed. I wanted to experiment using the Random class to create the random numbers, as opposed to math.random*100.
I'm getting an error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at CreateArray.main(CreateArray.java:11)
Here's my code:
import java.util.Scanner;
import java.util.Random;
public class CreateArray{
public static void main(String [] args){
int n = 10;
Random myrandom = new Random();
int[] engine = new int[n]; // Initialising my array and giving it the name engine, I'm also passing value 'n' to my array length
engine[n] = myrandom.nextInt(100-0); //This exclues 100 and 0, to include those I would say +100 or +0
for(int i=0; i < engine.length; i++){ //Using a for loop to cycle through my array and print out its elements
System.out.println(engine[i]); //Printing out my array
}
}
}