Re: [J2SE] 請教把1~6隨意排序
※ 引述《rosemary (迷迭香)》之銘言:
: 大家好,第一次在此板發表文章,
: 有不周到之處請大家告訴我 ^^
: 我想要寫一隻隨意把連續的數字打散的程式,
: 例如1~6號,執行之後 可能變成 2,5,1,6,4,3
: 搜尋網路,看到很多很精彩的排序程式,
: 可是要把排序打散...不知道用什麼java語法可以達成想要的功能呢?
: 謝謝大家 ^^
後來寫出來了 ^^ 提供給大家參考
private static String[] shuffle(String[] team) {
int teamlength = team.length;
double[] temp = new double[teamlength];
double tempd;
String temps;
//先產生亂數,分配給每一組
for(int i=0;i<teamlength;i++){
temp[i] = Math.random();
}
//對亂數排序,順便就把組順序調動,
for(int i=0;i<teamlength;i++) {
for(int j=teamlength-1;j>i;j--) {
if(temp[j]<temp[j-1]) {
tempd = temp[j-1];
temp[j-1] = temp[j];
temp[j] = tempd;
temps = team[j-1];
team[j-1] = team[j];
team[j] = temps;
}
}
}
return team;
}
public static void main(String[] args){
String team[]={"第一組","第二組","第三組","第四組","第五組","第六組"};
shuffle(team);
for(String s:team){
System.out.println(s);
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.224.40.94
→
09/01 00:45, , 1F
09/01 00:45, 1F
噓
09/01 01:00, , 2F
09/01 01:00, 2F
推
09/01 01:02, , 3F
09/01 01:02, 3F
→
09/01 01:05, , 4F
09/01 01:05, 4F
→
09/01 01:06, , 5F
09/01 01:06, 5F
推
09/02 09:29, , 6F
09/02 09:29, 6F
→
09/02 13:10, , 7F
09/02 13:10, 7F
→
09/02 13:34, , 8F
09/02 13:34, 8F
→
09/02 13:34, , 9F
09/02 13:34, 9F
→
09/02 13:36, , 10F
09/02 13:36, 10F
→
09/02 13:38, , 11F
09/02 13:38, 11F
→
09/02 13:39, , 12F
09/02 13:39, 12F
→
09/02 13:40, , 13F
09/02 13:40, 13F
討論串 (同標題文章)