我有一些包含嵌套 if 语句的代码:
if(numberOfNeighbors == 1){
//go through comparison again, add Pixel(i,j) to current linked list -> complist[numberOfComponents]
// break out of large check ??
if(ji.getPixelColor(i, j) == (ji.getPixelColor(i-1,j-1))){ //compare to top left
complist[numberOfComponents].addFirst(new Pixel(i,j,numberOfComponents)); break;
}
if(ji.getPixelColor(i, j) == (ji.getPixelColor(i,j-1))){ // compare to top
complist[numberOfComponents].addFirst(new Pixel(i,j,numberOfComponents)); break;
}
if(ji.getPixelColor(i, j) == (ji.getPixelColor(i+1,j-1))){ // compare to top right
complist[numberOfComponents].addFirst(new Pixel(i,j,numberOfComponents)); break;
}
if(ji.getPixelColor(i, j) == (ji.getPixelColor(i-1,j))){ // compare to left
complist[numberOfComponents].addFirst(new Pixel(i,j,numberOfComponents)); break;
}
} // end of if(numberOfNeighbors == 1)
基本上我想做的,不管这可能效率低下,是比较某个东西 4 次,但如果结果是匹配,则跳出 4 个嵌套 if 语句的集合,以及外部 if 语句.
这行得通吗?或者如果它当前处于嵌套状态并继续到下一个直到它完成所有 4 个,它是否会脱离嵌套?