标题: 用Math.random生成1,2,3,4,5,6,7,8,9,10的随机数 不重复 [打印本页] 作者: fanlelong 时间: 2016-4-19 23:34 标题: 用Math.random生成1,2,3,4,5,6,7,8,9,10的随机数 不重复 public class Demo_Text3 {
/**
* 18、用Math.random生成1,2,3,4,5,6,7,8,9,10的随机数 不重复
*/
public static void main(String[] args) {
String s1 = "[";
StringBuffer sb = new StringBuffer("[");
for (int i = 1; i <= 10; ) {
int x =(int)((Math.random()*10)+1);
if (x==i) {
i++;
if (x==10) {
sb=sb.append(x).append("]");
}else{
sb=sb.append(x).append(",");
}
}
}
System.out.print(sb.toString());
int x=1;
String s ="[";
while (x<=10) {
int y =(int)((Math.random()*10)+1);
if (y==x) {
x++;
if (y==10) {
s=s+y+"]";
}else{
s=s+y+",";
}
}
}
System.out.println(s);
}