Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 转换语句&;用户输入_Java_Switch Statement_Maze - Fatal编程技术网

Java 转换语句&;用户输入

Java 转换语句&;用户输入,java,switch-statement,maze,Java,Switch Statement,Maze,我正在写一个程序,从电影《幽灵之旅》中移走一张志广的照片。我现在需要做的就是把她左右上下移动。她有一个用户输入的初始位置。然后,我的程序要求用户输入移动她的u/d/l/r。如何提示用户输入以再次移动她?它总是移动她,然后退出循环 // Initial position Scanner keyboard = new Scanner(System.in); System.out.print("Starting row: "); int currentRow = keyboard.nextInt();

我正在写一个程序,从电影《幽灵之旅》中移走一张志广的照片。我现在需要做的就是把她左右上下移动。她有一个用户输入的初始位置。然后,我的程序要求用户输入移动她的u/d/l/r。如何提示用户输入以再次移动她?它总是移动她,然后退出循环

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

}

将代码放在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!");}}
  }
}

使用以下代码,您可以随意移动,当您想退出程序时,只需键入“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');

编辑:更正

您需要在开关盒周围设置一个
do-while
循环。