实现思路:生成50-100之间的随机数(包括50,包含100),之后循环k次分别输出即可:
int k = 5;
for(int i = 0;i Random rand = new Random(); int randNum = rand.nextInt(50)+50; System.out.println(randNum ); } 实现思路:获取到最大数减去最小数的值作为随机生成的数,之后得到的值加上最小数,就是所需要的数值。
jdk要求1.5或以上
import java.util.ArrayList;
public class MathTest {
public static void main(String[] args) {
prinRan(ran(10));
}
public static ArrayList ran(int k) {
ArrayList al = new ArrayList();
for (int i = 0; i < k; i++) {
al.add((int) (Math.random() * 50 + 50));
}
return al;
}
public static void prinRan(ArrayList li) {
for (Object o : li) {
System.out.println(o);
}
}
}
public class T1 {
public static void main(String[] args) {
int k = 10 ;
int[] num = new int[k];
for(int i = 0;i
}
print(num);
}
public static int rodem(){
int i = (int)(Math.random()*100) ;
if(i>50){
return i ;
}
return rodem();
}
public static void print(int[] n){
for(int i = 0;i
System.out.print(",") ;
}
System.out.print(n[i]) ;
}
}
}
结果为:
73,87,97,71,90,70,98,61,76,75 (随即的)