所以我知道这个代码采用一个随机的10个数字数组,并按从低到高的顺序排列。它怎么做呢?你能解释一下循环,缓冲区以及我和J的东西吗?
import java.util.Arrays;
public class Divisible {
public static void main(String[] args) {
int[] array = new int [10];
//generates 10 Random numbers in the range of 1-20
for (int i = 0; i < array.length; i++ ) {
array[i] = (int)(Math.random()*20 + 1);
}
System.out.println(Arrays.toString(array));
int buffer = 0;
for(int i1 = array.length-1; i1 > 0; i1--) {
for(int j = 0 ; j < i1 ; j++) {
if(array[j] > array[j+1]) {
buffer = array[j];
array[j] = array[j+1];
array[j+1] = buffer;
}
}
}
System.out.println(Arrays.toString(array));
}
}
答案 0 :(得分:1)