看板 Ajax 關於我們 聯絡資訊
程式碼如下: var random = new Array(); random[0] = Math.floor(Math.random()*10); random[1] = Math.floor(Math.random()*10); random[2] = Math.floor(Math.random()*10); random[3] = Math.floor(Math.random()*10); random[4] = Math.floor(Math.random()*10); random[5] = Math.floor(Math.random()*10); random[6] = Math.floor(Math.random()*10); random[7] = Math.floor(Math.random()*10); random[8] = Math.floor(Math.random()*10); random[9] = Math.floor(Math.random()*10); for(var i = 0 ; i < 10 ; i++){ if(random[i] == random[i+1]){ random[i+1] = Math.floor(Math.random()*10); i--; continue; } } 我覺得外面好像還要在一層迴圈,但是沒有頭緒,有前輩可以給我一些idea嗎? 我現在目前的程式只能做到鄰近的值是不相同的,但是沒辦法做到跳號的值是不同的 ex.temp[0]跟temp[2],總覺得邏輯思考有點亂。 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 42.72.188.69 ※ 文章網址: https://www.ptt.cc/bbs/Ajax/M.1426835149.A.5BA.html
mmis1000: http://jsbin.com/liyaso 03/20 15:22
mmis1000: 先把要出現的數字選出來在洗牌阿 03/20 15:23
swallowcc: 把值塞進array裡再shuffle... 03/20 16:15
swallowcc: 大概像這樣 http://goo.gl/6xayg 03/20 16:15
FunkyBass: 謝謝兩位大大,已解決,洗牌這事情比我原先想的好太多 03/20 16:27