[問題] 隨機排列
小弟在練習陣列的時候,做下面這個題目
將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)
→
11/23 15:44, , 1F
11/23 15:44, 1F
→
11/23 15:54, , 2F
11/23 15:54, 2F