-3
你必須創建兩個獨立的程序,一個主程序和一個戰列艦類。其他人正在讓主程序詢問用戶猜測座標,您可以輸入設置變量來僞裝成用戶的猜測。如何在元素中搜索2d數組?
公共類MainMethod {
public static void main(String[] args) {
Battleship_BraydenH_R1 var = new Battleship_BraydenH_R1();
//initialize variables
int[][] LAYOUT = new int[7][7];
int ROW = 1, COLUMN = 2;
Battleship_BraydenH_R1.hitDetect(ROW, COLUMN);
}
}
公共類Battleship_BraydenH_R1 {
/**
*Program Header
*Program Name: Battleship
*Program Description: This class tells the main method whether or not a ship has been hit or whether or not a ship has been sunk.
*Program Creator: Brayden H
*Revision 1
*Date: May 9, 2016
*/
public static void batClass()
{
// initialize variables
int[][] LAYOUT = {
{0,0,0,1,0,0,0},
{0,0,1,0,0,0,0},
{0,1,0,1,0,0,1},
{1,0,0,1,0,1,0},
{0,0,0,1,1,0,0},
{0,0,0,1,0,0,0},
{0,0,1,0,0,0,0},
}; // layout array
} // batClass
public static boolean[] hitDetect (int X, int Y)
{
int[] counter = new int[3]; // counter array
//counter[0] = boat[0];
boolean HIT = false, SUNK = false; // sets 'HIT' and 'SUNK' booleans to false
boolean[] STATUS = new boolean [2]; // array with 2 elements to be able to return 'HIT' and 'SUNK'
STATUS[0] = false; // MISS
STATUS[1] = true; // HIT
// if the user guessed the correct coordinates for boat 1
if ((X == 3 && Y == 4)||(X == 4 && Y == 4)||(X == 5 && Y == 4))
{
STATUS[1] = true; // the boat was hit
X = 4; // turns 1 into 4 so the program knows when a ship is sunk
Y = 4; // turns 1 into 4 so the program knows when a ship is sunk
} // boat 1
// if the user guessed the correct coordinates for boat 2
else if ((X == 1 && Y == 4)||(X == 2 && Y == 3)||(X == 3 && Y == 2)||(X == 4 && Y == 1))
{
STATUS[1] = true; // the boat was hit
X = 4; // turns 1 into 4 so the program knows when a ship is sunk
Y = 4; // turns 1 into 4 so the program knows when a ship is sunk
} // boat 2
// if the user guessed the correct coordinates for boat 3
else if ((X == 3 && Y == 7)||(X == 4 && Y == 6)||(X == 5 && Y == 5)||(X == 6 && Y == 4)||(X == 7 && Y == 3))
{
STATUS[1] = true; // the boat was hit
X = 4; // turns 1 into 4 so the program knows when a ship is sunk
Y = 4; // turns 1 into 4 so the program knows when a ship is sunk
} // boat 3
// if a boat was not hit
else
{
STATUS[1] = false; // the boat was not hit
}
(THIS IS WHERE I WANT THE PROGRAM TO SEARCH THE LAYOUT)
// searches layout
double a[][]=new double[7][7];
for(int row = 0 ; row < 3 ; row++)
{
for(int col = 0 ; col < 7 ; col++)
{
}
}
return STATUS; // returns 'STATUS' to main method
} // hitDetect method
} //戰艦類
請一些文本添加到您的問題。以易於理解的方式解釋您的問題,而不是通過代碼註釋來解決問題,這將有助於您獲得更好的答案。 –
完成,希望解釋幫助! –