作者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)
→ tkcn:你前面的數字印出來之後,又跟後面的數字做交換了 11/23 15:44
→ xshane831:謝謝我看到了 print放錯地方 感謝!! 11/23 15:54