因此,我正在采用一种方法,在 Java10x10格格网上,由2-5平方米的船舶组成。 这种方法将计算所有“*”船舶在电网上的沉积船舶数目,船舶将标记为“S”和水”。 我已经这样做了,但在适当计算沉积船只数目时,我却继续陷入一个问题,因为它要么把一艘星号沉船(即一艘有效船舶)作为沉积船,但最近我却陷入了把所有星星体作为沉积船的问题。 如你在下面的船上看到的那样,本应将1艘船舶作为船上唯一有效的船舶归还,但在我操作这种方法时,该船正在返回4艘。 在计算船上/电网上岸的船舶数量时,这一问题也随之出现。
这里是理事会/网络:
static char[][] board = {
{ . , . , . , . , . , . , . , . , . , . },
{ . , . , S , . , . , * , * , . , . , . },
{ . , . , . , . , . , . , . , . , . , . },
{ . , . , . , . , . , . , . , . , . , . },
{ . , . , . , . , . , . , . , . , . , . },
{ . , . , . , . , . , * , . , . , . , . },
{ . , . , . , . , . , * , . , . , . , . },
{ . , . , . , . , . , . , . , . , . , . },
{ . , . , . , . , . , . , . , . , . , . },
{ . , . , . , . , . , . , . , . , . , . }
};
public static int sunkenShips(char boardArray[][]) {
board = boardArray;
int sunkenShips = 0; //counts the amount of sunken ships
boolean sunken = false; //used later to check if the the space is a sunken ship
for(char[] row : boardArray) { //loops through board array
for(int i = 0; i < row.length; i++) { //loops through each row
if(row[i] == * ) { //checks if current element is a sunken ship
sunken = true; //sets it to true then
int ii = 0; //resets so it can check if it is a one star (not a sunken ship) in each row
for(int j = i; j < row.length && j < i + 5; j++) { //if it is sunken it checks whether its a valid sunken ship (2-5 squares long), it will go on until the 5th square and/or until the end of the row
ii++;
if(row[j] != * && ii == 1) { //checks if the next square after the one is was on is a * and if not which would make it only 1 star and not a sunken ship, break out of the loop
sunken = false; //changes it to false therefore
break;
}
}
if(sunken == true) { //if the ship is sunken then increments to the sunken ship variable
sunkenShips++;
}
}
}
}
if (validBoard(boardArray) == 12 && sunken == true) { //checks if board is valid using other method and if board has sunken ships
return sunkenShips;
}
else if (validBoard(boardArray)==12) {
for(char[] row: boardArray) {
for(int i = 0; i < row.length; i++) { //goes through the array to check if there aren t any ships
if (row[i] == S ) { //checks if there is a ship
boolean validShip = true; //used to checking if its a valid ship (2-5 squares in length)
int iii = 0; //resets so it can check if it is a one S (not a ship) in each row
for(int j = i; j < row.length && j < i + 5; j++) { // same logic as above
iii++;
if(row[j] != S && iii == 1) { //same logic as above to check if its a ship
validShip = false;
break;
} else {
validShip = true;
}
}
if (validShip == false) { // if there aren t ships it should return this value
return 0;
}
}
}
}
}
return 2; //default return value
}
我试图通过下一个4个空间对法典的这一部分进行大量敲响,以检查其有效作战船舶是否有效,并检查其是否有效(至少2个空间)。 但是,无论何时,它都未能通过声明或最近未能妥善地通行。
for(int j = i; j < row.length && j < i + 5; j++) { //if it is sunken it checks whether its a valid sunken ship (2-5 squares long), it will go on until the 5th square and/or until the end of the row
ii++;
if(row[j] != * && ii == 1) { //checks if the next square after the one is was on is a * and if not which would make it only 1 star and not a sunken ship, break out of the loop
sunken = false; //changes it to false therefore
break;
}
}
这里指的是任何想:
<编码> 采用一种方法,当给一家有效的委员会时(见上文——假定投入委员会有效)将沉船数目退回。 如果船上没有船舶(这仍是一个有效的船上),则应归还价值。