Java将长if语句更改为for循环
我有一个if语句,看起来像这样:Java将长if语句更改为for循环,java,for-loop,if-statement,Java,For Loop,If Statement,我有一个if语句,看起来像这样: if (pan[x + 1][y + 1].getBackground() == TeamColor && pan[x + 1][y].getBackground() == TeamColor && pan[x + 1][y -1].getBackground() == TeamColor &&a
if (pan[x + 1][y + 1].getBackground() == TeamColor &&
pan[x + 1][y].getBackground() == TeamColor &&
pan[x + 1][y -1].getBackground() == TeamColor &&
pan[x][y - 1].getBackground() == TeamColor &&
pan[x - 1][y - 1].getBackground() == TeamColor &&
pan[x - 1][y].getBackground() == TeamColor &&
pan[x - 1][y + 1].getBackground() == TeamColor &&
pan[x][y + 1].getBackground() == TeamColor) {
// do something
}
boolean match = true;
for (int dx = -1; match && (dx < 2); ++dx) {
for (int dy = -1; match && (dy < 2); ++dy) {
if (dx != 0 || dy != 0) {
match = pan[x+dx][y+dy].getBackground() == TeamColour;
}
}
}
if (match) {
// do something
}
目标是检查当前x和y值周围的每个项目(在2d数组中),并确保它们是正确的颜色
我想有一个简单的方法可以做到这一点。我假设为创建一个循环可以通过迭代每个项目来解决问题,但不幸的是,我没有想到一种方法来实现这一点,因为这些项目不是按顺序排列的
注意:我在stackoverflow上发现了许多其他帖子,标题是“SolutionforveryLongIf语句”,不幸的是,它们使用不同的编程语言(如python、android和javascript)
注2:这不是邮政的副本。这是一个字符串和正则表达式的问题,不幸的是,这不是我问题的解决方案
希望有人会有答案 试试这样的方法:
if (pan[x + 1][y + 1].getBackground() == TeamColor &&
pan[x + 1][y].getBackground() == TeamColor &&
pan[x + 1][y -1].getBackground() == TeamColor &&
pan[x][y - 1].getBackground() == TeamColor &&
pan[x - 1][y - 1].getBackground() == TeamColor &&
pan[x - 1][y].getBackground() == TeamColor &&
pan[x - 1][y + 1].getBackground() == TeamColor &&
pan[x][y + 1].getBackground() == TeamColor) {
// do something
}
boolean match = true;
for (int dx = -1; match && (dx < 2); ++dx) {
for (int dy = -1; match && (dy < 2); ++dy) {
if (dx != 0 || dy != 0) {
match = pan[x+dx][y+dy].getBackground() == TeamColour;
}
}
}
if (match) {
// do something
}
布尔匹配=true;
对于(int-dx=-1;匹配&(dx<2);++dx){
对于(int-dy=-1;匹配&(dy<2);++dy){
如果(dx!=0 | | dy!=0){
match=pan[x+dx][y+dy].getBackground()==teamcolor;
}
}
}
如果(匹配){
//做点什么
}
基本上,您需要检查每个方向上的偏移量-1、0和1,因此我们有两个for
循环,每个循环在一个维度上产生这些偏移量。然后,我们检查对应于每个偏移量的数组元素,并使用match
变量进行跟踪
但是请注意,与原始代码一样,这将在边界附近失败(例如,如果x==0
)。如有必要,可对此进行修复
当然,也可以让循环在实际索引上运行以进行检查(例如,(int x2=x-1;x2
)。最后也差不多。试试这样的方法:
if (pan[x + 1][y + 1].getBackground() == TeamColor &&
pan[x + 1][y].getBackground() == TeamColor &&
pan[x + 1][y -1].getBackground() == TeamColor &&
pan[x][y - 1].getBackground() == TeamColor &&
pan[x - 1][y - 1].getBackground() == TeamColor &&
pan[x - 1][y].getBackground() == TeamColor &&
pan[x - 1][y + 1].getBackground() == TeamColor &&
pan[x][y + 1].getBackground() == TeamColor) {
// do something
}
boolean match = true;
for (int dx = -1; match && (dx < 2); ++dx) {
for (int dy = -1; match && (dy < 2); ++dy) {
if (dx != 0 || dy != 0) {
match = pan[x+dx][y+dy].getBackground() == TeamColour;
}
}
}
if (match) {
// do something
}
for (int a = x-1;a <= x+1;a++)
{
if (a < 0 || a >= pan.length) continue;
for (int b = y-1; b <= y+1; b++)
{
if (b < 0 || b >= pan[a].length) continue;
if (a == x && b == y) continue;
if (pan[a][b].getBackground() != TeamColor)
return false;
}
}
return true;
布尔匹配=true;
对于(int-dx=-1;匹配&(dx<2);++dx){
对于(int-dy=-1;匹配&(dy<2);++dy){
如果(dx!=0 | | dy!=0){
match=pan[x+dx][y+dy].getBackground()==teamcolor;
}
}
}
如果(匹配){
//做点什么
}
基本上,您需要检查每个方向上的偏移量-1、0和1,因此我们有两个for
循环,每个循环在一个维度上产生这些偏移量。然后,我们检查对应于每个偏移量的数组元素,并使用match
变量进行跟踪
但是请注意,与原始代码一样,这将在边界附近失败(例如,如果x==0
)。如有必要,可对此进行修复
当然,也可以让循环在实际索引上运行以进行检查(例如,(int x2=x-1;x2for(inta=x-1;a=pan.length)continue;
for (int a = x-1;a <= x+1;a++)
{
if (a < 0 || a >= pan.length) continue;
for (int b = y-1; b <= y+1; b++)
{
if (b < 0 || b >= pan[a].length) continue;
if (a == x && b == y) continue;
if (pan[a][b].getBackground() != TeamColor)
return false;
}
}
return true;
对于(int b=y-1;b=pan[a]。长度)继续;
如果(a==x&&b==y)继续;
如果(平移[a][b].getBackground()!=TeamColor)
返回false;
}
}
返回true;
对于(int a=x-1;a=pan.length)continue;
对于(int b=y-1;b=pan[a]。长度)继续;
如果(a==x&&b==y)继续;
如果(平移[a][b].getBackground()!=TeamColor)
返回false;
}
}
返回true;
我可以提出两种方法:
1) 全对象方式
您可以引入一个自定义类坐标,该类包含两个值:x坐标和y坐标。
创建一个坐标列表,其中包含要测试的坐标元素,并对其进行迭代以满足需要。
public class Coordinate{
private final int x;
private final int y;
public Coordinate(int x, int y){
this.x = x;
this.y = y;
}
public getX(){
return x;
}
public getY(){
return y;
}
}
您可以使用它:
List<Coordinate> coordinates = new ArrayList<>();
coordinates.add(new Coordinate(1,1));
coordinates.add(new Coordinate(1,0));
coordinates.add(new Coordinate(1,-1));
coordinates.add(new Coordinate(0,-1));
coordinates.add(new Coordinate(-1,-1));
coordinates.add(new Coordinate(-1,0));
coordinates.add(new Coordinate(-1,1));
coordinates.add(new Coordinate(0,1));
// you can also init them with a loop
boolean isMatched = true;
for (Coordinate coordinate : coordinates){
if (pan[x + coordinate.getX()][y + coordinate.getY()].getBackground() != TeamColor){
isMatched = false;
break;
}
}
列表坐标=新的ArrayList();
坐标。添加(新坐标(1,1));
坐标。添加(新坐标(1,0));
坐标。添加(新坐标(1,-1));
坐标。添加(新坐标(0,-1));
坐标。添加(新坐标(-1,-1));
坐标。添加(新坐标(-1,0));
坐标。添加(新坐标(-1,1));
坐标。添加(新坐标(0,1));
//您还可以使用循环初始化它们
布尔值isMatched=true;
用于(坐标:坐标){
如果(平移[x+坐标.getX()][y+坐标.getY()].getBackground()!=TeamColor){
isMatched=假;
打破
}
}
对象方式更详细,但它有公开规则的优势。
因此,您可以轻松阅读和更改它。
假设要检查的规则变得更加复杂,它就变得非常有价值
2) 短码方式
即使通过内联坐标值并忽略您不想测试的特定情况(无更改情况),逻辑上也是一样的。
boolean isMatched=true;
对于(int xDelta=-1;xDelta我可以提出两种方法:
1) 全对象方式
您可以引入一个自定义类坐标,该类包含两个值:x坐标和y坐标。
创建一个坐标列表,其中包含要测试的坐标元素,并对其进行迭代以满足需要。
public class Coordinate{
private final int x;
private final int y;
public Coordinate(int x, int y){
this.x = x;
this.y = y;
}
public getX(){
return x;
}
public getY(){
return y;
}
}
您可以使用它:
List<Coordinate> coordinates = new ArrayList<>();
coordinates.add(new Coordinate(1,1));
coordinates.add(new Coordinate(1,0));
coordinates.add(new Coordinate(1,-1));
coordinates.add(new Coordinate(0,-1));
coordinates.add(new Coordinate(-1,-1));
coordinates.add(new Coordinate(-1,0));
coordinates.add(new Coordinate(-1,1));
coordinates.add(new Coordinate(0,1));
// you can also init them with a loop
boolean isMatched = true;
for (Coordinate coordinate : coordinates){
if (pan[x + coordinate.getX()][y + coordinate.getY()].getBackground() != TeamColor){
isMatched = false;
break;
}
}
列表坐标=新的ArrayList();
坐标。添加(新坐标(1,1));
坐标。添加(新坐标(1,0));
坐标。添加(新坐标(1,-1));
坐标。添加(新坐标(0,-1));
坐标。添加(新坐标(-1,-1));
坐标。添加(新坐标(-1,0));
坐标。添加(新坐标(-1,1));
坐标。添加(新坐标(0,1));
//您还可以使用循环初始化它们
布尔值isMatched=true;
用于(坐标:坐标){
如果(平移[x+坐标.getX()][y+坐标.getY()].getBackground()!=TeamColor){
isMatched=假;
打破
}
}
对象方式更详细,但它有公开规则的优势。
因此,您可以轻松阅读和更改它。
假设要检查的规则变得更加复杂,它就变得非常有价值
2) 短码方式
即使通过内联坐标值并忽略您不想测试的特定情况(无更改情况),逻辑上也是一样的。
boolean isMatched=true;
对于(int-xDelta=-1;xDelta@Mac-yes