我有2 for循环,我正在做一个2048喜欢的游戏。如果有东西滑向左侧,此方法返回true。但是,它会返回false,只有“banana”正在打印,这意味着其他for循环甚至不会执行。可能是什么情况?我的第二个循环没有运行?
public boolean slideLeft(int[][] array, int row) {
int[] array2 = new int[array.length];
boolean result = false;
for(int j = 0; j < array.length; j++) {
array2[j] = array[j][row];
System.out.println("banana");
}
for (int i = 0; i <array2.length; i ++) {
if (array2[i] == 0 && array2[i+1] != 0 && i != array2.length-1) {
System.out.println("apple");
array2[i] = array2 [i+1];
result = true;
}
if(array2[i] != 0 && i != 0 && array2[i-1] == array2[i]) {
System.out.println("pear");
array2[i-1] = 2*(array2[i-1]);
array2[i]=0;
result = true;
}
}
return result;
}
或者您的其他循环正在运行,但您的条件都不符合。你的条件应该测试什么? – 2014-10-31 04:17:01
启动您的调试器并逐步执行。你可以检查每个变量并找出发生了什么。 – 2014-10-31 04:19:01
或者,在你的两个'if'中创建'else',并在那里打印一些内容,比如'System.out.println(“不是苹果”);'。 – 2014-10-31 04:20:40