我想在事件发生时从“for循环”中退出。例如,我的代码有一部分:
for (ScanResult scanResult : listeScan) {
if(sampleposition[position]==0){
change=true;
while(i < position && change==true){
if(macAdress[i].equals(scanResult.BSSID)){
change = false;
}
i++;
}
i=0;
if(change == true && scanResult.level >= -80){
valueSSID[position] = scanResult.level;
macAdress[position] = scanResult.BSSID;
sSID[position] = scanResult.SSID;
if(valueSSID[position]!=0){sampleposition[position]++;}
//exit here
}
}
else{
if(macAdress[position].equals(scanResult.BSSID) && sampleposition[position]<=totalsample){addlevel=scanResult.level;} else {addlevel=0;}
valueSSID[position] = valueSSID[position] + addlevel;
if(addlevel!=0){sampleposition[position]++;}
//exit here
}
}
}
实际上,我想停止循环,其中有评论“退出此处”。
拜托,你能告诉我怎么办?对不起我的英文......
答案 0 :(得分:10)
break;
这完全退出for循环。
continue;
这将继续循环的下一次迭代。
答案 1 :(得分:5)
您正在寻找break
关键字,这将立即结束循环:
for (ScanResult scanResult : listeScan) {
if(sampleposition[position]==0){
change=true;
while(i < position && change==true){
if(macAdress[i].equals(scanResult.BSSID)){
change = false;
}
i++;
}
i=0;
if(change == true && scanResult.level >= -80){
valueSSID[position] = scanResult.level;
macAdress[position] = scanResult.BSSID;
sSID[position] = scanResult.SSID;
if(valueSSID[position]!=0){sampleposition[position]++;}
break;
}
}
else{
if(macAdress[position].equals(scanResult.BSSID) && sampleposition[position]<=totalsample){addlevel=scanResult.level;} else {addlevel=0;}
valueSSID[position] = valueSSID[position] + addlevel;
if(addlevel!=0){sampleposition[position]++;}
break;
}
}
}
答案 2 :(得分:1)
只需使用关键字
即可break;
你想要停止循环
for (ScanResult scanResult : listeScan) {
if(sampleposition[position]==0){
change=true;
while(i < position && change==true){
if(macAdress[i].equals(scanResult.BSSID)){
change = false;
}
i++;
}
i=0;
if(change == true && scanResult.level >= -80){
valueSSID[position] = scanResult.level;
macAdress[position] = scanResult.BSSID;
sSID[position] = scanResult.SSID;
if(valueSSID[position]!=0){sampleposition[position]++;}
break;
}
}
else{
if(macAdress[position].equals(scanResult.BSSID) && sampleposition[position]<=totalsample){addlevel=scanResult.level;} else {addlevel=0;}
valueSSID[position] = valueSSID[position] + addlevel;
if(addlevel!=0){sampleposition[position]++;}
break;
}
}
}
答案 3 :(得分:0)