【发布时间】:2020-03-11 11:36:32
【问题描述】:
我只是想更改我的代码,以便在每次运行代码时生成一个固定长度为 100 个整数的随机数组,而不仅仅是在代码中包含一个预设数组。我对此很陌生,所以只需要一个正确方向的指南,谢谢
public class Selectionsort {
public static void main(String[] args) {
int[] numbers = {15, 8, 6, 21, 3, 54, 6, 876, 56, 12, 1, 4, 9};
sort(numbers);
printArray(numbers);
}
public static int[] sort(int[] A) {
for (int i = 0; i < A.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < A.length; j++) {
if (A[j] < A[minIndex]) {
minIndex = j;
}
}
int temp = A[minIndex];
A[minIndex] = A[i];
A[i] = temp;
}
return A;
}
public static void printArray(int[] A) {
for (int i = 0; i < A.length; i++) {
System.out.println(A[i]);
}
}
}
【问题讨论】:
-
显然你已经知道循环了。所以你需要弄清楚的是如何在Java中生成随机整数。当您在 Google 上搜索“如何在 Java 中生成随机整数”时会发生什么?