2015-04-22 137 views
-2

我不能手動測試我的方法的代碼,因爲我還沒有完成其他相關的代碼,但我覺得方法public boolean isFilledAt(int row, int col)的邏輯是錯誤的。如果該形狀在給定的行/列位置具有填充的塊(任何char),並且如果該塊爲空(點'.'),則返回truefalse。如果位置超出範圍,請提供帶有信息性消息的FitItException。請問請看看它,讓我知道如果方法的代碼有什麼問題?謝謝!不確定代碼的邏輯

public class CreateShape { 

    private int height; 
    private int width; 
    private char dc; 
    private Rotation initialPos; 
    private char[][] shape = new char[height][width]; 
public boolean isFilledAt(int row, int col) 
    { 
     if(row < 0 || row >= height || col < 0 || col >= width) 
      throw new FitItException("Oops! Out of bounds!"); 
     else 
     { 
      for (int i = 0; i < shape.length; i++) 
       for (int j = 0; j < shape[i].length; j++) { 
        if (shape[row][col] == dc) 
         return true; 
       } 
     } 
     return false; 
    } 
+1

你需要迭代嗎? –

+0

:) :),簡直想不到! @PrashantGhimire謝謝,我現在要改變它! – Nikolay

回答

1
public boolean isFilledAt(int row, int col) { 
    if(row < 0 || row >= height || col < 0 || col >= width) 
     throw new FitItException("Oops! Out of bounds!"); 
    else 
     return (shape[row][col] == dc); 
} 

確實爲你的代碼目前不相同。我不確定你是通過陣列進行的,你甚至沒有使用ij變量。