编写一个java程序,生成10个1000~9999之间的随机整数,将其输出到控制台

2025-03-23 17:59:49
推荐回答(4个)
回答1:

public class Test {
public static void main(String[] args) {
for(int i = 0;i < 10 ; i++){
//Math.random()的取值范围是[0,1)
//Math.random() * 9000的取值范围是[0,8999]
//Math.random() * 9000 + 1000的取值范围是[1000,9999]
int num = (int)(Math.random() * 9000 + 1000);
System.out.println(i+1 + "==>" +num);
}
}
}

回答2:

import java.util.Random;

public class Test{
     public static void main(String[] args) {
          Random ran = new Random();
           for(int i=0;i<10;i++){
              int num = ran.nextInt(9000)+1000;
              System.out.print(num+"\t");
             }
       }
}

回答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:

public class MathRandom {
public static void main(String[] args){
for(int i = 0;i < 10;i++){
int randomnum = (int)(Math.random()*9000+1000);
System.out.println(randomnum);
}
}
}