Java 如果语句未按预期工作
基本上,我目前正在尝试为Android制作一款reversi游戏,我的if语句让我有点头疼,似乎如果条件适合不止一个,那么它只是在其中一个语句上做动作,然后离开另一个语句。我的代码如下所示:Java 如果语句未按预期工作,java,android,if-statement,reversi,Java,Android,If Statement,Reversi,基本上,我目前正在尝试为Android制作一款reversi游戏,我的if语句让我有点头疼,似乎如果条件适合不止一个,那么它只是在其中一个语句上做动作,然后离开另一个语句。我的代码如下所示: if (check[position] == 0 && (check[position - 8] == 2 || check[position + 8] == 2
if (check[position] == 0
&& (check[position - 8] == 2
|| check[position + 8] == 2
|| check[position + 1] == 2
|| check[position - 1] == 2
|| check[position - 9] == 2
|| check[position + 9] == 2
|| check[position - 7] == 2 || check[position + 7] == 2)) {
if (check[position + 8] == 2) {
for (int i = position; i < 56; i += 8) {
if (check[i] == 1) {
for (int j = position; j < i; j += 8) {
check[j] = 1;
}
playerno = 2;
break;
} else
break;
}
} else if (check[position - 8] == 2) {
for (int i = position; i > 8; i -= 8) {
if (check[i] == 1) {
for (int j = position; j > i; j -= 8) {
check[j] = 1;
}
playerno = 2;
break;
} else
break;
}
} else if (check[position + 1] == 2) {
for (int i = position; i < board.length; i++) {
if (check[i] == 1) {
for (int j = position; j < i; j++) {
check[j] = 1;
}
playerno = 2;
break;
}
if (i == 7 || i == 15 || i == 23 || i == 31
|| i == 39 || i == 47 || i == 55
|| i == 63) {
break;
}
}
} else if (check[position - 1] == 2) {
for (int i = position; i > 0; i--) {
if (check[i] == 1) {
for (int j = position; j > i; j--) {
check[j] = 1;
}
playerno = 2;
break;
}
if (i == 0 || i == 8 || i == 16 || i == 24
|| i == 32 || i == 40 || i == 48
|| i == 56) {
break;
}
}
}
if(检查[位置]==0
&&(检查[位置-8]==2
||检查[位置+8]==2
||检查[位置+1]==2
||检查[位置-1]==2
||检查[位置-9]==2
||检查[位置+9]==2
||检查[位置-7]==2 | |检查[位置+7]==2)){
如果(检查[位置+8]==2){
对于(int i=位置;i<56;i+=8){
如果(检查[i]==1){
对于(int j=位置;j8;i-=8){
如果(检查[i]==1){
对于(int j=位置;j>i;j-=8){
检查[j]=1;
}
playerno=2;
打破
}否则
打破
}
}否则如果(检查[位置+1]==2){
用于(int i=位置;i0;i--){
如果(检查[i]==1){
对于(int j=位置;j>i;j--){
检查[j]=1;
}
playerno=2;
打破
}
如果(i==0 | | i==8 | | i==16 | | i==24
||i==32 | | i==40 | | i==48
||i==56){
打破
}
}
}
Check只是一个int数组,它记录了由于某种原因,现在哪个玩家在棋盘上持有特定的棋子,如果我有一个位置满足其中两个条件,它只会通过其中一个if语句,而这通常会导致游戏将其视为无效的移动,我想知道如何才能绕过它?First,您的条件极其复杂且难以读取。请尝试将其简化。尝试将一个复杂条件拆分为多个更简单的表达式。并使用调试器。首先,您的条件极其复杂且难以读取。请尝试将其简化。尝试将一个复杂条件拆分为多个更简单的表达式并使用调试器 如果我有一个位置满足其中两个条件 执行其中一个if语句 你指的是这份声明吗
if (conditionA) {
BlockA
} else if (conditionB) {
BlockB
} else if (conditionC) {
BlockC
} else if (conditionD) {
BlockD
}
如果执行,难怪只执行其中一个If块。只执行第一个条件的块,该条件的计算结果为true
如果要允许执行多个块,请将其更改为:
if (conditionA) {
BlockA
}
if (conditionB) {
BlockB
}
if (conditionC) {
BlockC
}
if (conditionD) {
BlockD
}
如果我有一个位置满足其中两个条件
执行其中一个if语句
你指的是这份声明吗
if (conditionA) {
BlockA
} else if (conditionB) {
BlockB
} else if (conditionC) {
BlockC
} else if (conditionD) {
BlockD
}
如果执行,难怪只执行其中一个If块。只执行第一个条件的块,该条件的计算结果为true
如果要允许执行多个块,请将其更改为:
if (conditionA) {
BlockA
}
if (conditionB) {
BlockB
}
if (conditionC) {
BlockC
}
if (conditionD) {
BlockD
}
AlexR是对的,您的逻辑太复杂,而且格式设置使得代码非常难以阅读,这使得调试变得非常困难 我不打算为你解决整个问题,但这里有一些建议的改变。最重要的是,你应该把你的逻辑分解成小块 编辑:根据我上面的评论,将董事会作为一个类来实现:
class Board {
private final int[] check = new int[BOARD_WIDTH*BOARD_HEIGHT];
public Board() { for (int i=0; i < BOARD_WIDTH*BOARD_HEIGHT; check[i++] = 0); }
public final int get(int x, int y) { return check[y*BOARD_WIDTH + x]; }
public final void set(int x, int y, int val) { check[y*BOARD_WIDTH+x] = val; }
/**
* Return true if this square is free
*/
public final boolean isFree(int x, int y) {
if (x < 0 || x >= BOARD_WIDTH || y < 0 || y >= BOARD_HEIGHT) return false;
int position = y*BOARD_WIDTH + x;
return check[position] == 0;
}
/**
* Return true if this square is occupied by opponent
*/
public final boolean isOccupiedBy2(int x, int y) {
if (x < 0 || x >= BOARD_WIDTH || y < 0 || y >= BOARD_HEIGHT) return false;
int position = y*BOARD_WIDTH + x;
return check[position] == 2;
}
/**
* Return true if any neighboring square is occupied by opponent
*/
final boolean isNeighborOccupied(int x, int y) {
for (int i=x-1; i >= x+1; ++i)
for (int j=y-1; j >= y+1; ++j)
if ((i != x || j != y) && isOccupiedBy2(i,j)) return true;
return false;
}
// etc.
}
看看这有多容易阅读?调试也会容易得多
最后,看看Eran的帖子是否解决了你的bug。这四个条件中只有一个会被执行。因为你只测试了八个相邻方块中的四个,我猜你是想测试上、左、下、右,所以也许第二个“else”这是一个错误。AlexR是对的,您的逻辑太复杂,而且格式设置使您的代码非常难以阅读,这使得调试非常困难 我不打算为你解决整件事,但他