生成固定长度的随机数组(Java) [英] Generating random array of fixed length (Java)
本文介绍了生成固定长度的随机数组(Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是想更改我的代码,以便每次运行该代码时都会生成一个固定长度为100的整数的随机数组,而不是在代码中仅包含一个预先设置的数组.我对此很陌生,所以只需要一个正确方向的指南,谢谢
im just looking to change my code so that a random array of a fixed length of 100 integers is generated every time the code is ran rather than just have a pre-set array within the code. Im quite new to this so just need a guide in the right direction, thanks
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]);
}
}
}
推荐答案
public class Selectionsort {
public static void main(String[] args) {
int[]numbers= new int[100]
Random random = new Random();
for(int i=0;i<numbers.length;i++){
numbers[i]=random.nextInt(100);
}
sort(numbers);
printArray(numbers);
}
上面的代码段将帮助您为大小为100的数组创建一个介于1到100之间的随机数.
The above snippet will help you to create a random numbers between 1 to 100 for the array of size 100.
这篇关于生成固定长度的随机数组(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文