简体   繁体   English

切换语句和用户输入

[英]switch statement & user input

I'm writing a program which moves a picture of Chichiro from the film "Spirited Away". 我正在编写一个程序,该程序可以从电影《千与千寻》中移动Chichiro的照片。 All I currently need to do is to move her left, right, up, and down. 我现在要做的就是向左,向右,向上和向下移动她。 She has an initial position which the user inputs. 她具有用户输入的初始位置。 Then my program asks for user input to move her u/d/l/r. 然后我的程序要求用户输入以移动她的u / d / l / r。 How do I prompt for user input to move her AGAIN? 如何提示用户输入再次移动她? It always just moves her and exits the loop. 它总是只会移动她并退出循环。

// 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!");

}

Put your code in a while loop, and include a means to exit, like hitting the q key: 将您的代码放入while循环中,并包括退出方法,例如按q键:

 boolean quit=false;

 //keep asking for input until a 'q' is pressed
 while(! quit) {
   System.out.print("Move Chichiro (u/d/l/r/q): ");
   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;
     case 'q': quit=true; // quit playing
               break;
     default: System.out.print("That is not a valid direction!");}}
  }
}

WIth the following code you can move as much as you want and when you want to quit the program, you just have to type 'q' : 使用以下代码,您可以随意移动任意数量,而要退出该程序,只需键入“ q”:

        // Create maze
    Maze maze = new Maze(numberRows, numberCols, currentRow, currentCol);
    char move;


    do{

            System.out.print("Move Chichiro (u/d/lr): ");

        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!");

        }

    }while(move != 'q');

EDIT : Correction 编辑:更正

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM