2013-03-11 57 views
-5

我有一個java程序,我有一點難度,並會真正感謝幫助。創建三種方法,更改值? java

  • addMine方法(應該改變the_minefield的值(在元件thisCol,thisRow)從EMPTY_SQUARE到MINE_SQUARE,並返回值真。)

-addMine方法(如果輸入參數位於外雷區的尺寸,那麼該方法不應該改變該類的狀態,並返回錯誤。)

-addMine方法(如果在輸入參數指示的方塊中已經有一個地雷,那麼再次方法不應該改變類的狀態,並返回false。)

-getValue方法(應返回請求的行和列處的雷區值。 (目前,雷區中的值是MINE_SQUARE或EMPTY_SQUARE)。如果行和列的謊言雷區之外,此方法應返回0)

-addMinesToCorners(應該做的事情,它說,加4枚水雷)

class MineFinderModel 
{ 
public static int MINE_SQUARE = 10; 
public static int EMPTY_SQUARE = 0; 

int num_of_cols; 
int num_of_rows; 
int[][] the_minefield; 

public MineFinderModel(int n_cols, int n_rows) 
{ 
    num_of_rows = n_rows; 
    num_of_cols = n_cols; 
    the_minefield = new int[num_of_cols][num_of_rows]; 
} 

public boolean addMine(int thisCol, int thisRow) 
{ 

    return false; // but you sometimes need to return true; 
} 
public int getValue(int thisCol, int thisRow) 
{ 

    return 0; // but you need to return other numbers at times; 
} 
public void addMinesToCorners() 
{ 
} 

}

+6

問題是什麼? – 2013-03-11 11:02:32

+0

[email protected] – 2013-03-11 11:04:11

+0

我需要創建這些方法,我真的很煩惱,請你能給我發電子郵件嗎? – 2013-03-11 11:04:47

回答

0
public void addMinesToCorners() 
{ 
/*Adding mines to four corners*/ 
the_minefield[0]0]    = "Corner Mine1" 
the_minefield[0][num_of_cols -1] = "Corner Mine2" 
the_minefield[num_of_rows-1][0] = "Corner Mine2" 
the_minefield[num_of_rows -1][num_of_cols -1] = "Corner Mine2" 
} 
1
public boolean addMine(int thisCol, int thisRow) 
{ 
    if(thisCol >= n_cols || thisRow >= n_rows) 
     return false; 
    if(the_minefield[thisCol][thisRow] == MINE_SQUARE) 
     return false; 
    the_minefield[thisCol][thisRow] = MINE_SQUARE; 
    return true; 
} 
public int getValue(int thisCol, int thisRow) 
{ 
    if(thisCol >= n_cols || thisRow >= n_rows) 
     return 0; 
    return the_minefield[thisCol][thisRow]; 
} 
public void addMinesToCorners() 
{ 
    the_minefield[0][0] = MINE_SQUARE; 
    the_minefield[0][n_rows - 1] = MINE_SQUARE; 
    the_minefield[n_cols - 1][0] = MINE_SQUARE; 
    the_minefield[n_cols - 1][n_rows - 1] = MINE_SQUARE; 
} 
+0

編譯時似乎有9個錯誤 – 2013-03-11 11:16:56

+0

有什麼錯誤? – uba 2013-03-11 11:28:23

+0

應該是'num_of_rows'和'num_of_cols'。 – Boann 2013-03-11 12:04:53

相關問題