繁体   English   中英

Java-根据用户输入在2d数组中移动位置(并打印出当前位置)

[英]Java - Move position in 2d Array based on user input (and print out current position)

我有一个程序,用户可以在其中写入命令U,D,R,L。 根据用户命令,我希望位置以2D阵列移动。 例如,如果用户写UDUDU,那么我希望该职位“上”,“下”,“上”,“下”,“上”。 到目前为止,我只设法“一次”移动它。 我想我必须更新当前位置,在这里我遇到了麻烦,需要帮助!

例如,使用决定大小[3] [3]和位置[1] [1]。

  0 1 2 
0 0 0 0
1 0 1 0
2 0 0 0

如果用户按下UDUDU,则新位置将像这样(在这种情况下,x为+1)

  0 1 2 
0 0 0 0
1 0 0 0
2 0 1 0

这是代码:

 Scanner scan = new Scanner(System.in);

 System.out.println("Enter length(x) and width(y)");
                    x = scan.nextInt();
                    y = scan.nextInt();

  int[][] roomsize = new int[x][y];//The array

  System.out.println("Enter the starting position");
                    x = scan.nextInt();
                    y = scan.nextInt();

  roomsize[x][y] = 1; //Starting position


System.out.println("Enter commands U(up), D(down), R(right) or  L(left)");//User input          
                   String command = scan.next();    

for (char directionCommand : command.toCharArray()){
                      move(directionCommand, roomsize, x, y);

                     }
                //Should I print it out here? It   
                /*for(int i = 0; i < roomsize.length; i++)
                   {
                      for(int j = 0; j < roomsize[i].length; j++)
                        {
                            System.out.print(roomsize[i][j]);
                            if(j < roomsize[i].length - 1)     System.out.print(" ");
                        }
                        System.out.println();
                    }*/


public static void move(int i, int[][] roomsize, int x, int y){
                    int px = 0;//Current position
                    int py = 0;//Current position
                    switch(i){
                  case 'U': px+=1; break;//Is this the right way to update position?
                  case 'D': roomsize[x-1][y]; break;//Or this waY?
                  case 'R': roomsize[x][y+1] = 1; break;
                  case 'L': roomsize[x][y-1] = 1;break;                
                  default:; break;
                  }             
                    roomsize[px][py] = 1;//Do I set the new position like this?

 for(int i1 = 0; i1 < roomsize.length; i1++)
                       {
                          for(int j = 0; j < roomsize[i1].length; j++)
                            {
                                System.out.print(roomsize[i1][j]);
                                if(j < roomsize[i1].length - 1) System.out.print(" ");
                            }
                            System.out.println();
                        }  

以及我何时何地需要“打印”数组中的位置? 在switch语句之后执行此操作似乎有错误吗? 要么? 在此先感谢大家!

有道理,您只能移动一次。 用户输入位于for循环之外。

String command = scan.next(); 

在循环之前读取,然后将命令的长度用作for循环

for (char directionCommand : command.toCharArray())

我会尝试使用while循环

 Scanner scan = new Scanner(System.in);

 System.out.println("Enter length(x) and width(y)");
                    x = scan.nextInt();
                    y = scan.nextInt();

  int[][] roomsize = new int[x][y];//The array

  System.out.println("Enter the starting position");
                    x = scan.nextInt();
                    y = scan.nextInt();

  roomsize[x][y] = 1; //Starting position


while(true){
System.out.println("Enter commands U(up), D(down), R(right),  L(left), and Q(quit)");//User input
    String command = scan.next();
if(command.equalsIgnoreCase("Q"){
    break;
}
    move(command.charAt(0), roomsize, x, y);
}

暂无
暂无

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

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