Java: сортировка списка случайной ярости числа + организация его (BubbleSort) + получение наибольшего сгенерированного числа - PullRequest
0 голосов
/ 16 апреля 2020

Я изо всех сил пытаюсь завершить проект, и я был бы признателен за чью-либо помощь. Заранее спасибо.

Требования: 1. Распечатать список из 10 случайных чисел в диапазоне от (1-100), Готово. 2. Распечатать сгенерированный список в полумесяце; 3. Напечатайте наибольшее число из сгенерированного списка.

Ниже приведено то, что я сделал и где я застрял:

import java.util.Arrays;
import java.util.Random;

public class Project {

    public static void main(String[] args) {

        list(0, 0);
        printMax(0);
        //bubbleSort();
        System.out.print("The sorted list is: "); //???

    }

    private static void list(int min, int max) {

        int[] numbers = new int[10];
        // Generates 10 Random Numbers in the range 1 -100
        for (int i = 0; i<numbers.length; i++) {
            numbers[i] = (int)(Math.random() * 100 + 1);
        }
        System.out.println("The unsorted list is: " + Arrays.toString(numbers));
        return;

    }

    private static void printMax(int...numbers) {
        int result = numbers[0];

        for (int i = 1; i<numbers.length; i++) {

            if (numbers[i] > result)
                result = numbers[i];
        }
        System.out.println("The largest value is " + numbers);
        return;
    }

    public static void bubbleSort(int[] list) {
        int temp;

        for (int i = list.length - 1; i > 0; i--) {
            for (int j = 0; j<i; j++) {
                if (list[j] > list[j + 1]) {
                    temp = list[j];
                    list[j] = list[j + 1];
                    list[j + 1] = temp;
                }
            }
        }
    }
}

1 Ответ

0 голосов
/ 20 апреля 2020

// Моисес Исайя да да Силва

publi c class Project_chapter_7 {

public static void main(String[] args) {

    int[] my_list = generateRandomNumbers(1, 100);
    printMax(my_list);
    bubbleSort(my_list);

}
/*The function random of the class Math generate an random double number between 0 and 1.0.
 * So, if we want an number between 1 and 100, we just multiply the value for 100 
 * (to get the maximum of random numbers) and add 1 at the end,
 * to exclude the 0 case
 * */
private static int[] generateRandomNumbers(int min, int max) {

    int[] numbers = new int[10];

    // Generates 10 Random Numbers in the range 1 -100
    for (int i = 0; i < numbers.length; i++) {
        numbers[i] = (int) (Math.random() * max + min);
    }

    System.out.print("The unsorted list is: ");
    printValuesFromArray(numbers);

    return numbers;

}

private static void printValuesFromArray(int[] numbers) {
    for (int i = 0; i < numbers.length; i++) {
        System.out.print(numbers[i] + " ");
    }
}
//Now we find the largest number, comparing each number of the list (generateRandomNumbers)
private static void printMax(int... numbers) {
    int result = numbers[0];

    for (int i = 1; i < numbers.length; i++) {

        if (numbers[i] > result) {
            result = numbers[i];
        }
    }

    System.out.println("\nThe largest value is " + result);
    return;
}

public static void bubbleSort(int[] list) {
    int temp;

    //The first value of i is the last value of the array, for this case, the first i will be 9
    //(because the index of the last number on array is 9)
    //i represent the outside loop, and will be decrement every time until reach the 0 value

    for (int i = list.length - 1; i > 0; i--) {
        for (int j = 0; j < i; j++) {
            if (list[j] > list[j + 1]) {
                temp = list[j];
                list[j] = list[j + 1];
                list[j + 1] = temp;
            }
        }
    }
    System.out.print("The sorted list is: ");
    printValuesFromArray(list);
}

}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...