Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 Can';t中断嵌套for循环_Actionscript 3_Arrays_Loops_Match_Break - Fatal编程技术网

Actionscript 3 Can';t中断嵌套for循环

Actionscript 3 Can';t中断嵌套for循环,actionscript-3,arrays,loops,match,break,Actionscript 3,Arrays,Loops,Match,Break,我有以下函数,但尽管使用了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++

我有以下函数,但尽管使用了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...")

                            }
                        }
                    }

                }
private函数CheckMatch(){
//_playersList是循环查找匹配项的数组
变量i:int;
var j:int;
对于(i=0;i<\u playersList.length;i++){
对于(j=i+1;j<\u playersList.length;j++){
如果(_playersList[i]===_playersList[j]){
跟踪(“匹配:“+i+”处的“+”playersList[i]+”与“+j”处的“+”playersList[j]+”匹配);
打破
}否则{
//没有对手
跟踪(“继续…”)
}
}
}
}

中断一次只会中断一个循环(或开关)。

添加一个名为find initialized to false的布尔变量

从更改循环条件

i < _playersList.length
i<\u playersList.length

i<\u playersList.length&!建立
然后在你休息之前,设置found=true

啊……我明白了

使用了标签,现在可以使用了:

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...")

                        }
                    }
                }

            }
private函数CheckMatch(){
//_playersList是循环查找匹配项的数组
变量i:int;
var j:int;
OuterLoop:for(i=0;i<\u playersList.length;i++){
对于(j=i+1;j<\u playersList.length;j++){
如果(_playersList[i]===_playersList[j]){
跟踪(“匹配:“+i+”处的“+”playersList[i]+”与“+j”处的“+”playersList[j]+”匹配);
断开外环;
}否则{
//没有对手
跟踪(“继续…”)
}
}
}
}

我认为还有另一种代码更少的解决方案:

private function checkMatch():void {
    for (var i : int = 0; i < _playerList.length-1; i++) {
        if (_playerList.indexOf(_playerList[i], i+1) > i) {
            break;
        }
    }
}
private函数checkMatch():void{
for(变量i:int=0;i<\u playerList.length-1;i++){
if(_playerList.indexOf(_playerList[i],i+1)>i){
打破
}
}
}

你说“似乎没有停止”是什么意思?它是输出匹配吗?你用调试器检查过了吗?我的意思是,根据跟踪语句,我仍然看到在早期跟踪显示匹配后“继续”。哦,是的,我猜op可能意味着打破两个循环,在这种情况下,你对问题的看法是对的。这个答案值得解决-对我来说似乎更干净。
private function checkMatch():void {
    for (var i : int = 0; i < _playerList.length-1; i++) {
        if (_playerList.indexOf(_playerList[i], i+1) > i) {
            break;
        }
    }
}