java 怎么随机取出5个0——10的不同随机数?要不同的

来源:百度知道 编辑:UC知道 时间:2024/05/28 08:55:12
java 怎么随机取出5个0——10的不同随机数?要不同的
要不同的啊
是整数

import java.util.Random;

class GetRandom{
public static void main(String args[]){
Random random1 = new Random();
for(int i =0;i<5;i++){
int x = random1.nextInt(10);
System.out.println(x);
}
}
}
楼上的简化,不知是否有问题,请楼上看下
还有就是未解决重复问题
等会……待会回来 。
随机数应该是整数吧

~~~~~~~~~~~~~修正版~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

import java.util.Random;

class test {

/**
* @param args
*/
public static void main(String[] args) {//随机取出5个0-10的 不同 随机数(整数),若带有浮点数可自行修改
// TODO Auto-generated method stub
Random random1 = new Random();
int count = 0;
int[] cache =new int[5];
while(count<5){
int x = random1.nextInt(10);
if(!isEqual(x,cache)){
//count++;
cache[count]=x;
count++;
System.out.println(x);
}
else;
}
}
private static Boolean isEqual(i