用JAVA随机生成10个100以内的整数,案后按从小到大排列打印出来 求结果图 谢谢

如题所述

第1个回答  2012-05-27

public class Demo {

public static void main(String[] args){

int a[] = new int[10];

int temp;

for(int i =0; i < 10; i++){

a[i] = (int)(Math.random()*100);

}

for(int i = 0; i < 10; i++){

for(int j = i+1; j < 10; j++){

if(a[i] > a[j]){

temp = a[i];

a[i] = a[j];

a[j] = temp;

}

}

}

for(int i = 0; i < 10; i++){

System.out.print(a[i] + "   ");

}

}

}

本回答被提问者采纳
第2个回答  2012-05-27
import java.util.Random;

public class Rand {
public static void main(String[] args) {
//声明长度为10的数组
int num[]=new int[10];
//得到0-99以内的10个随机数,并将其保存到数组中
for (int i = 0; i < num.length; i++) {
Random rand=new Random();
num[i]=rand.nextInt(100);
}
//将产生的10个随机数进行从小到大的排序
for (int i = 0; i < num.length; i++) {
for (int j = 0; j < num.length-i-1; j++) {
if(num[j]>num[j+1]){
int temp=0;
temp=num[j];
num[j]=num[j+1];
num[j+1]=temp;
}
}
}
//最后将排好序的数组打印出来
System.out.println("将产生的10个随机数进行从小到大的排序:");
for (int i = 0; i < num.length; i++) {
System.out.print(num[i]+"\t");
}
}

}
第3个回答  2012-05-27
public static void main(String[] args) {
// TODO Auto-generated method stub
int al[]=new int [10];
Random r=new Random();
for(int i=0;i<10;i++){
int a=r.nextInt(100);
//System.out.println(a);
al[i]=a;
}
for(int i=0;i<10;i++){
Arrays.sort(al);
System.out.println(al[i]);
}
}
相似回答