Java:对列表的随机数字范围进行排序+组织它(冒泡排序)+获取最大的生成数字

标签 java arrays random project bubble-sort

我正在努力完成一个项目,非常感谢任何人的帮助。提前致谢。

要求: 1. 打印一个包含 (1-100) 范围内的 10 个随机数的列表,完成。 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;
                }
            }
        }
    }
}

最佳答案

//莫伊塞斯·伊萨亚斯·达席尔瓦

公共(public)类(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);
}

}

关于Java:对列表的随机数字范围进行排序+组织它(冒泡排序)+获取最大的生成数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61250066/

相关文章:

java - 投影矩阵的作用不仅仅是缩放,对吗?

Java ArrayList 内容不传播

c - 如何随机化 64 位模式中的最低位

r - 在 R 中,如何在节点之间随机生成边?

python - 生成随机字符串并检查是否等于静态字符串[Python]

java - 返回与原始对象相同的对象的副本

java - 如何通过 REST API 在事件中心发送和使用消息

javascript - jQuery 检查值是否在对象中

arrays - golang结构数组转换

java - 在最近的单词边界截断字符串