jeudi 17 juin 2021

(Java) Creating array the size of a user input, then filling with random numbers

I am trying to create an array which it's size is determined based on a user input, then fill the array with random numbers between -1000 and 1000.

The array needs to be called to various sorting methods, such as a selection sort.

I am receiving an exception in my line where I try to initialize each variable in the array.

for (int i = 0; i < arr.length; i++){

I get the error "Illegal start of type," and "cannot find 'i'"

arr can't be found in my sorting methods or anywhere else in my program as well.

Any clue what I did wrong? Here is my code.

    public class array{
        int input = Integer.parseInt(sortIn.getText());
        int[] arr = new int[input];
        Random random = new Random();
        
        for (int i = 0; i < arr.length; i++){
        arr[i] = (random.nextInt(1000)-1000);
        }
    }




Aucun commentaire:

Enregistrer un commentaire