所以我試圖創建一個程序,它遍歷2D數組,直到找到匹配的票據;然而,當程序發現陣列內的數字它不會中斷:突破循環的問題
var namesArray = ["Reece", "Jack", "Lewis", "Ryan"];
var raffleArray = [
[193, 109, 97, 479],
[489, 046, 387, 830],
[475, 947, 294, 590],
[330, 395, 750, 649]
];
//function getRandomNum(){
// return Math,floor(Math.random() * (999 -))
//}
//loops through the 2D array to search for
for (j = 0; j < 5; j++) {
for (i = 0; i < raffleArray[j].length; i++) {
if (raffleArray[j][i] == 294) { //294 is only set for testing
alert(namesArray[j] + " has won the raffle with ticket number: " + raffleArray[j][i]);
break;
} else {
alert("Ticket: " + raffleArray[j][i]+ " did not win"} //only for testing
}
}}
希望有人能幫助!
歇只會打破你內for循環.. – Keith
'爲(J = 0;Ĵ<5; J ++)''namesArray'has只有4個成員,所以不宜它是'Ĵ<4' ,甚至更好'j
connexo
你的'j <5'也是錯的,..只有4個元素..也許'j
Keith