输入一个个位数,这个数代表产生的随机数的位数,并且随机数的每一位都不能与其它位重复,比如当输入5时,打印出13245 或者45683,每一位不能重复。
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class Test2 {
public static void main(String[] args) {
String result = printRandomMath(9);
System.out.println(result);
}
public static String printRandomMath(int x){
Map pool = new HashMap();
Random random = new Random();
StringBuffer result = new StringBuffer();
for (int i = 0; i < x; i++) {
int temp = random.nextInt(9);
if(pool.containsValue(temp)){
x++;
}else{
pool.put(i, temp);
result.append(temp);
result.append("--");
}
}
return result.toString();
}
}