请帮忙编写下 从100中随机抽取10个数 然后再由大到小排序这些数

来源:百度知道 编辑:UC知道 时间:2024/05/23 00:37:19
从100中随机抽取10个数 然后再由大到小排序这些数
用4个for语句与一个if语句编写的

public class A
{
public static void main(String[] args)
{
int temp[]=new int[10];
for(int i=0;i<temp.length;i++)
{
temp[i]=(int)(Math.random()*100)+1;
}
for(int i=0;i<temp.length;i++)
{
for(int j=i+1;j<temp.length;j++)
{
if(temp[j]>temp[i])
{
int info=temp[i];
temp[i]=temp[j];
temp[j]=info;
}
}
}
for(int i=0;i<temp.length;i++)
{
System.out.println(temp[i]);
}
}
}

4个for循环 1个if语句.

Random ran = new Random();
int[] intArray = new int[10];

for (int i = 0; i < 10; i++) {
//生成是个随机数,范围0-100;
intArray[i] = ran.nextInt(100);
System.err.print(intArray[i] + " ");
}

int temp;//冒泡排序中使用的中间变量

//两个循环,冒泡排序
for (int i = 0; i < 10; i++){
for (int j = 9; j > i; j--){
if (intArray[j] &g