java,哪位大神能帮忙在下面的代码里加一段,能把输出的20个数输从小到大排序,谢谢
importjava.lang.*;//importjava.util.Random;classUniqueRandomIntArray{//staticRandomrn...
import java.lang.*;
// import java.util.Random;
class UniqueRandomIntArray {
// static Random rnGen = new Random();
private static void uriArray(int[] rray) {
int low = 0;
int high = 99;
int rn;
int haveit = 0;
int i = 0;
int j;
while((haveit == 0) && i < rray.length) {
rn = randNum(low, high);
for(j = 0; j <= i; j++) {
if(rn == rray[j]) {
haveit = j;
j = i;
}
}
if(haveit != 0) {
System.out.println("a[" + haveit + "] is " + rn + " already");
haveit = 0;
}
else {
System.out.println("a[" + i + "] is " + rn);
rray[i] = rn;
i++;
}
} // end while
} // end uriArray
private static int randNum(int min, int max) {
int range = (max - min) + 1;
// int randnum = rnGen.nextInt(range) + min;
int randnum = (int) (Math.random() * range) + min;
return randnum;
}
public static void main(String[] args) {
int[] arra = new int[20];
uriArray(arra);
for(int i=0; i<arra.length; i++) {
System.out.print(" | " + arra[i]);
}
System.out.println(" | ");
}
} //class ends 展开
// import java.util.Random;
class UniqueRandomIntArray {
// static Random rnGen = new Random();
private static void uriArray(int[] rray) {
int low = 0;
int high = 99;
int rn;
int haveit = 0;
int i = 0;
int j;
while((haveit == 0) && i < rray.length) {
rn = randNum(low, high);
for(j = 0; j <= i; j++) {
if(rn == rray[j]) {
haveit = j;
j = i;
}
}
if(haveit != 0) {
System.out.println("a[" + haveit + "] is " + rn + " already");
haveit = 0;
}
else {
System.out.println("a[" + i + "] is " + rn);
rray[i] = rn;
i++;
}
} // end while
} // end uriArray
private static int randNum(int min, int max) {
int range = (max - min) + 1;
// int randnum = rnGen.nextInt(range) + min;
int randnum = (int) (Math.random() * range) + min;
return randnum;
}
public static void main(String[] args) {
int[] arra = new int[20];
uriArray(arra);
for(int i=0; i<arra.length; i++) {
System.out.print(" | " + arra[i]);
}
System.out.println(" | ");
}
} //class ends 展开
2个回答
展开全部
import java.util.Arrays;//加上头文件
public static void main(String[] args) {
int[] arra = new int[20];
uriArray(arra);
Arrays.sort(arra); //添加这一行
for (int i = 0; i < arra.length; i++) {
System.out.print(" | " + arra[i]);
}
System.out.println(" | ");
}
望采纳哦~~
输出:
a[0] is 12
a[1] is 9
a[2] is 88
a[3] is 20
a[4] is 94
a[1] is 9 already
a[5] is 86
a[6] is 30
a[7] is 62
a[8] is 51
a[9] is 36
a[10] is 32
a[11] is 92
a[8] is 51 already
a[12] is 31
a[13] is 19
a[14] is 60
a[15] is 33
a[16] is 39
a[17] is 46
a[18] is 42
a[19] is 45
| 9 | 12 | 19 | 20 | 30 | 31 | 32 | 33 | 36 | 39 | 42 | 45 | 46 | 51 | 60 | 62 | 86 | 88 | 92 | 94 |
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询