I have a method which asks the user to choose a number between 1 and 7. It uses the Scanner class to take the users input as an int. I saw how to code this for a string, but how would I modify that for int? My method is...
/** * This method asks the player to choose a column 1-7. If the player enters a number
- outside this range
- the method will continually ask until a correct number is entered. The column is
- decreased by one to account for
-
arrays starting at 0 and returned
-
@param turn Player which ever players turn it is, is asked for a column
-
@return int the column is returned after lowered by one */ public static int askForColumn(Player turn) {
System.out.println("\n\n" + turn.getTag() + " please enter the column number, 1-7, \n" + "where you would like to drop your game piece. "); boolean colCorrect = false; int column = -1; while (!colCorrect){ if(Connect4TextConsole.in.hasNextInt()){ column = Connect4TextConsole.in.nextInt(); colCorrect = true;} else{ System.out.println("Please enter a number 1 through 7."); Connect4TextConsole.in.next();}} while(column < 1 || column > 7) { System.out.println("Please enter a number 1 through 7."); column = Connect4TextConsole.in.nextInt(); } return column - 1; // subtract one to account for array starting at zero }
-
Aucun commentaire:
Enregistrer un commentaire