java 随机生成50个0-100的数,并按从小到大顺序排列

要显示23 65 2 5 6 89 2 5 6 23 65 89
2025-02-22 08:59:22
推荐回答(2个)
回答1:

public class User{  
 public static void main(String[] args) {
  int[] nums = new int[50];
  int count=0;
  while(count<50) {
   nums[count]=(int)(Math.random()*100);
   count++;
  }
  java.util.Arrays.sort(nums);//从小到大排序
  System.out.println("长度"+nums.length);//数组长度
  for (int i = 0; i < nums.length; i++) {
   System.out.print(nums[i]+"\t");
   if(i!=0 && (i+1)%5==0){
    System.out.println();//每5个换一行输出
   }
  }
 }
}

我这边的运行结果:

回答2:

建议你可以放到String[] 或是相关 阵列内
String arr[]=new String[10];
Arrays.sort(arr);<----由小到大排列