我有以下功能但是尽管使用了break语句,它在数组中找到匹配后似乎没有停止:
private function CheckMatch() {
// _playersList is the Array that is being looped through to find a match
var i:int;
var j:int;
for (i= 0; i < _playersList.length; i++) {
for (j= i+1; j < _playersList.length; j++) {
if (_playersList[i] === _playersList[j]) {
trace("match:" + _playersList[i] + " at " + i + " is a match with "+_playersList[j] + " at " + j);
break;
} else {
// no match
trace("continuing...")
}
}
}
}
答案 0 :(得分:10)
啊......我明白了。
使用标签,现在可以使用:
private function CheckMatch() {
// _playersList is the Array that is being looped through to find a match
var i:int;
var j:int;
OuterLoop: for (i= 0; i < _playersList.length; i++) {
for (j= i+1; j < _playersList.length; j++) {
if (_playersList[i] === _playersList[j]) {
trace("match:" + _playersList[i] + " at " + i + " is a match with "+_playersList[j] + " at " + j);
break OuterLoop;
} else {
// no match
trace("continuing...")
}
}
}
}
答案 1 :(得分:2)
添加一个名为found initialized的bool var为false。
从
更改循环条件i < _playersList.length
到
i < _playersList.length && !found
然后在休息之前,设置found = true
答案 2 :(得分:1)
break
一次只能打破一个循环(或切换)。
答案 3 :(得分:0)
我认为还有另一种解决方案,代码更少:
private function checkMatch():void {
for (var i : int = 0; i < _playerList.length-1; i++) {
if (_playerList.indexOf(_playerList[i], i+1) > i) {
break;
}
}
}