mardi 29 octobre 2019

How to insert random numbers into an array and sort them

My code is supposed to take the random number generated in the random method and sort them but it's only giving me one number.

My program is a random number generator that is supposed to make 1000 numbers that I can sort but my code only inserts one number into the array.

public static void main(String[] args) {
    // write

    int max = 1000;
    int min=0;
    int range = max - min + 1;

    // generate random numbers within 1 to 10
    for (int i = 0; i < 1000; i++) {
        int rand = (int) (Math.random () * range) + min;
        System.out.println ( rand );




        int array[] = {rand};
        int size = array.length;

        for ( i = 0; i < size - 1; i++) {
            int min1 = i;

            for (int j = i + 1; j < size; j++) {
                if (array[j] < array[min1]) {
                    min = j;
                }
            }
            int temp = array[min1];
            array[min1] = array[i];
            array[i] = temp;
        }

        for (int k = 0; k < size; i++) {
            System.out.print(" " + array[i]);
        }
    }
}



Aucun commentaire:

Enregistrer un commentaire