永发信息网

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

答案:5  悬赏:40  手机版
解决时间 2021-03-04 18:49
  • 提问者网友:了了无期
  • 2021-03-04 01:34
用JAVA程序编辑随机生成10个[100,1000]的整数
最佳答案
  • 五星知识达人网友:大漠
  • 2021-03-04 02:22
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);
}
}
}
全部回答
  • 1楼网友:有你哪都是故乡
  • 2021-03-04 06:46
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
  • 2楼网友:痴妹与他
  • 2021-03-04 05:22
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();
}
}
  • 3楼网友:鸠书
  • 2021-03-04 04:39
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] + " ");
}
}

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
特点:
可以灵活的传入下边界、上边界和生成随机数的个数
希望采纳!
  • 4楼网友:第四晚心情
  • 2021-03-04 03:37
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 + ",");
}
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯