這是我的代碼的一小部分。事情是,不管代碼將進入那個if
聲明。Java「缺少return語句」
public static double value(char[][] array, int x, int y) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
if (array[i][j] == (char) 120) {
int x_cord = i;
int y_cord = j;
int width = (x_cord - x);
int height = (y_cord - y);
Math.abs(width);
Math.abs(height);
double distance = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));
return distance;
}
}
}
}
我明白,我有一個合乎邏輯的缺陷在某處,我需要以某種方式,它會到達那裏不管是什麼,但我可以做到這一點告訴程序?
如果'array'是空的?在這種情況下你需要返回一些東西。或拋出異常。 – Tunaki
你必須從這個方法返回。現在,只有當口譯是真實的時候你纔會回來。 –