作者xshane831 (Shane)
看板java
标题[问题] 不重覆乱数
时间Tue Nov 23 15:33:13 2010
小弟在练习阵列的时候,做下面这个题目
将1~6数字随机排列,但是执行时总是会有重复的数字
我的目的应该是要不重覆,请问哪里错了?
class no3
{
public static void main(String[] args)
{
int x[]=new int[6];
int y=0;
for(int i=0; i<6; i++)
x[i]=i+1;
for(int j=0; j<6; j++){
int random = ((int)(Math.random()*6+1));
y=x[j];
x[j]=x[random-1];
x[random-1]=y;
System.out.println(x[j]);
}
}
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 115.43.220.53
※ 编辑: xshane831 来自: 115.43.220.53 (11/23 15:33)
1F:→ tkcn:你前面的数字印出来之後,又跟後面的数字做交换了 11/23 15:44
2F:→ xshane831:谢谢我看到了 print放错地方 感谢!! 11/23 15:54