我有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