用JAVA程序编辑随机生成10个[100,1000]的整数

2024-12-18 02:34:29
推荐回答(5个)
回答1:

import java.util.Random;

public class Main {

public static void main(String[] args){

int [] num = randNum(10, 100, 1000);
//冒泡排序
for (int i = 0; i < num.length; i++) {
for (int j = 0; j < num.length; j++) {
int temp;
if (num[i] > num[j]) {
temp = num[j];
num[j] = num[i];
num[i] = temp;
}
}
}
//打印
for (int i = 0; i < num.length; i++) {
System.out.print(num[i] + " ");
}
}

/**
* 产生min - max 范围内的 num 个随机数
* @param num 要产生的随机数的个数
* @param min 下边界
* @param max 上边界
* @return int[] 产生的随机数组
*/
public static int[] randNum(int num ,int min ,int max){
int result[] = new int[num];
Random rand = new Random();
for(int i = 0;i int rm=(rand.nextInt(max-min)+min);
result[i] = rm;
}
return result;
}

}
********打印出的降序结果***********
945 877 829 694 617 606 478 464 213 172

特点:
可以灵活的传入下边界、上边界和生成随机数的个数
希望采纳!

回答2:

package test;
import java.util.ArrayList;
import java.util.Collections;
public class demo1 {
public static void main(String[] args) {
// 定义集合
ArrayList list = new ArrayList();
// 生成10个随机数
for (int i = 0; i < 10; i++) {
list.add((int) (Math.random() * 900 + 100));
}
// 排序
Collections.sort(list);
// 逆转集合,以为是按升序排的
Collections.reverse(list);
// 输出
for (Integer n : list) {
System.out.print(n + ",");
}
}
}

回答3:

import java.util.ArrayList;

public class test {

public static void main(String[] args) {
//生成随机数
ArrayList randomNums = new ArrayList();
for (int i = 0; i < 10; i++) {
randomNums.add((int)(Math.random() * 900 + 100));
}

//排序输出
for (int i = 0; i < 10; i++) {
Integer max = 0;
for (Integer num : randomNums) {
if (num > max) {
max = num;
}
}
System.out.println(max);
randomNums.remove(max);
}
}
}

回答4:

import java.util.Random;

public class RandomTest {

public static void main(String[] args) {

Random rand = new Random();

final int[] nums = new int[10];

for(int i = 0; i < nums.length; i++){
nums[i] = 100 + rand.nextInt(900);
}

System.out.println("Before sorrting, the array is: ");
printAry(nums);

//sort
for(int i = 0; i < nums.length; i++){
for(int j = i; j < nums.length; j++){

if(nums[j] > nums[i]){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}

System.out.println("Sortted array is: ");
printAry(nums);
}

public static void printAry(int[] ary){
for(int i = 0; i < ary.length; i++){
System.out.print(ary[i] + " ");
}
System.out.println();
}

}
---------测试
Before sorrting, the array is:
814 805 333 753 617 745 751 649 803 949
Sortted array is:
949 814 805 803 753 751 745 649 617 333

回答5:

import java.util.Random;

public class RandomTest {

public static void main(String[] args) {

Random rand = new Random();

final int[] nums = new int[10];

for(int i = 0; i < nums.length; i++){
nums[i] = 100 + rand.nextInt(900);
}

System.out.println("Before sorrting, the array is: ");
printAry(nums);

//sort
for(int i = 0; i < nums.length; i++){
for(int j = i; j < nums.length; j++){

if(nums[j] > nums[i]){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}

System.out.println("Sortted array is: ");
printAry(nums);
}

public static void printAry(int[] ary){
for(int i = 0; i < ary.length; i++){
System.out.print(ary[i] + " ");
}
System.out.println();
}

}