gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:23:27 24 4
gpt4 key购买 nike

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

要求: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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com