Question: context is that I am building a sliding tile game in java, assume it is a 4x4 matrix built using a 2d array. Method Name:
context is that I am building a sliding tile game in java, assume it is a 4x4 matrix built using a 2d array.
Method Name:
getUserSelectedTile
Method inputs: nothing Method return: an integer number representing the tile game, such as 12 Method Functionality:Asks the use to enter the tile number via keyboard. Checks if the number is valid for the game (number must be between 1 and 15). This method keep asking the tile number until the entered number is valid. Bonus: if the user by mistake enters a letter along with a number (typo while entering the number, such as j12) your program should not abort with an exception. Instead the method should ask again for another number. You can do this by surrounding your code with Try and Catch as shown below:
private int getUserSelectedTile() { while (true) { try { // your code here } catch (Exception e) { // this line is just to clear the scanner buffer if needed // try to keep or remove the following line and see what // happens when you enter a bad tile number (such as k2) keyboard.nextLine();
} }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
