JAVA HELPPPPPPPPP this is not working.. for somereason and also need the bolded
ID: 3914433 • Letter: J
Question
JAVA HELPPPPPPPPP
this is not working.. for somereason
and also need the bolded part
CAN SOMEBODY HELP ME PLEASE!!!!!!!
/**
* This method interacts with the user to place a ship on the game board of the human player and
* the computer opponent. The process is as follows:
* 1 - Print the user primary board, using the printBoard.
* 2 - Using the promptChar method, prompt the user with "Vertical or horizontal? (v/h) ".
* A response of v is interpreted as vertical. Anything else is assumed to be horizontal.
* 3 - Using the promptInt method, prompt the user for an integer representing the
* "ship length", where the minimum ship length is Config.MIN_SHIP_LEN and the maximum
* ship length is width or height of the game board, depending on the input of the user
* from step 1.
* 4 - Using the promptStr method, prompt the user for the "x-coord". The maximum value
* should be calculated based on the width of the board and the length of the ship. You
* will need to use the coordAlphaToNum and coordNumToAlpha methods to covert between int
* and String values of coordinates.
* 5 - Using the promptInt method, prompt the user for the "y-coord". The maximum value
* should be calculated based on the width of the board and the length of the ship.
* 6 - Check if there is space on the board to place the ship.
* 6a - If so:
* - Place the ship on the board using placeShip.
* - Then, call placeRandomShip to place the opponents ships of the same length.
* - If placeRandomShip fails, print out the error message (terminated by a new
* line): "Unable to place opponent ship: id", where id is the ship id, and
* return false.
* 6b - If not:
* - Using promptChar, prompt the user with "No room for ship. Try again? (y/n): "
* - If the user enters a 'y', restart the process at Step 1.
* - Otherwise, return false.
*
* @param sc The Scanner instance to read from System.in.
* @param boardPrime The human player board.
* @param boardOpp The opponent board.
* @param id The ship id, assumed to be 1 to 9.
* @param rand The Random object.
* @return true if ship placed successfully by player and computer opponent, false otherwise.
*/
public static boolean addShip(Scanner sc, char boardPrime[][], char boardOpp[][], int id,
Random rand) {
printBoard(boardPrime, "My Ships");
char ch = promptChar(sc, "Vertical or horizontal? (v/h) ");
boolean dir = ch == 'v' ? true : false;
if (dir == true) {
int len = promptInt(sc, "ship length", Config.MIN_SHIP_LEN, boardPrime.length);
}
else {
int len = promptInt(sc, "ship length", Config.MIN_SHIP_LEN, boardPrime[0].length);
}
String xcoordStr = promptStr(sc, "x-coord", coordNumToAlpha(0), coordNumToAlpha(boardPrime[0].length - len));
// I get error on this len...
return false;
}
Explanation / Answer
You get error, because len is a local variable inside the if (dir == true) { block. you need to declare the len variable inside the function, not inside the if loop. Correct version:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.