我正在寫一個程序,它從電影「千與千尋」中移動奇契羅的圖片。我目前所需要做的就是移動她的左,右,上,下。她有一個用戶輸入的初始位置。然後我的程序要求用戶輸入移動她的u/d/l/r。我如何提示用戶輸入重新移動她?它總是讓她移動並退出循環。switch語句和用戶輸入
// Initial position
Scanner keyboard = new Scanner(System.in);
System.out.print("Starting row: ");
int currentRow = keyboard.nextInt();
System.out.print("Starting column: ");
int currentCol = keyboard.nextInt();
// Create maze
Maze maze = new Maze(numberRows, numberCols, currentRow, currentCol);
System.out.print("Move Chichiro (u/d/lr): ");
char move = keyboard.next().charAt(0);
switch (move){
case 'u': maze.moveTo(--currentRow, currentCol); // move up
break;
case 'd': maze.moveTo(++currentRow, currentCol); // move down
break;
case 'l': maze.moveTo(currentRow, --currentCol); // move left
break;
case 'r': maze.moveTo(currentRow, ++currentCol); // move right
break;
default: System.out.print("That is not a valid direction!");
}
您甲腎上腺素編輯'開關盒'周圍的'do-while'循環。 –