Java 如何让计数器在while循环中工作?

Java 如何让计数器在while循环中工作?,java,Java,所以我试着让我们假设intposx保留一个数字,然后当用户浏览菜单时,根据他们选择的内容,将向intposx添加1、0或-1。我无法让Posx在加或减后保留数字。我试图在一个数组中导航,我想保留2个整数作为标记,让我知道我当前在数组中关于x和y的位置 tl;dr如何在do/while循环中保持计数器 问题就在这里: //this variable will hold if we found or not the string in the puzzle boolean found

所以我试着让我们假设
intposx保留一个数字,然后当用户浏览菜单时,根据他们选择的内容,将向
intposx
添加1、0或-1。我无法让Posx在加或减后保留数字。我试图在一个数组中导航,我想保留2个整数作为标记,让我知道我当前在数组中关于x和y的位置

tl;dr如何在do/while循环中保持计数器

问题就在这里:

    //this variable will hold if we found or not the string in the puzzle
    boolean found = true;

    do {    

        //Print out the array
        for (int x = 0; x < maze.length; x++) {
            for (int y = 0; y < maze[0].length; y++) {
                System.out.print(maze[x][y]);
            }
            System.out.println();
        }

        System.out.printf("You may:\n1) Move up\n2) Move down\n3) Move left\n4) Move right\n0) Quit\nYour Choice (0-4):\n");
        int usrAns = sc2.nextInt();

        if (usrAns == 0){
            System.out.println("Bye!\n");
            found = false;
            break;
        }           

        //arrays to hold the direction.
        int[] movx ={ -1,  0, 0, 1};
        int[] movy ={ 0, -1, 1,  0};

        //Array to hold the position. 
        int Posx = 0;
        int Posy = 0;       

        if (usrAns == 1){
            if(check(Posx, Posy, maze, movx[1], movy[1])){
                    System.out.println("Cannot move past cave boundary! Try something else.\n");
                    continue;
                }
                else{
                    Posy = Posy - 1;
                    System.out.printf("This is Posx %d and Posy %d\n", Posx, Posy);
                    continue;
                }
        }
        if (usrAns == 2){
            if(check(Posx, Posy, maze, movx[2], movy[2])){
                    System.out.println("Cannot move past cave boundary! Try something else.\n");
                    continue;
                }
                else{
                    Posy= Posy + 1;
                    System.out.printf("This is Posx %d and Posy %d\n", Posx, Posy);
                    continue;
                }
        }
        if (usrAns == 3){
            if(check(Posx, Posy, maze, movx[0], movy[0])){
                    System.out.println("Cannot move past cave boundary! Try something else.\n");
                    continue;
                }
                else{
                    Posx = Posx - 1;
                    System.out.printf("This is Posx %d and Posy %d\n", Posx, Posy);
                    continue;
                }
        }
        if (usrAns == 4){
            if(check(Posx, Posy, maze, movx[3], movy[3])){
                    System.out.println("Cannot move past cave boundary! Try something else.\n");
                    continue;
                }
                else{
                    Posx =Posx + 1;
                    System.out.printf("This is Posx %d and Posy %d\n", Posx, Posy);
                    continue;
                }
        }
        while (usrAns >= 5 || usrAns < 0){
            System.out.println("Please enter a number between 0 and 4:\n");
            usrAns = sc2.nextInt();
            if (usrAns == 0){
                System.out.println("Bye!\n");
                found = false;
                break;
            }

        }
    }while(found);
//如果我们在拼图中找到或没有找到字符串,此变量将保持不变
布尔值=真;
做{
//打印出数组
对于(int x=0;x=5 | | usrAns<0){
System.out.println(“请输入一个介于0和4之间的数字:\n”);
usrAns=sc2.nextInt();
如果(usrAns==0){
System.out.println(“再见!\n”);
发现=错误;
打破
}
}
}while(发现);

任何想法、技巧或窍门都将不胜感激。

我认为问题在于这两条线

    //Array to hold the position. 
    int Posx = 0;
    int Posy = 0;   
每次迭代都要重置Posx和Posy。尝试将它们设置在while循环之外。就像这样(你不想一次又一次地初始化它们)。movx和movy相同:

//arrays to hold the direction.
int[] movx ={ -1,  0, 0, 1};
int[] movy ={ 0, -1, 1,  0};

//Array to hold the position. 
int Posx = 0;
int Posy = 0;       

do {
    //Print out the array
    for (int x = 0; x < maze.length; x++) {
    ...
    ...
//用于保持方向的数组。
int[]movx={-1,0,0,1};
int[]movy={0,-1,1,0};
//阵列以保持该位置。
int Posx=0;
int-Posy=0;
做{
//打印出数组
对于(int x=0;x
为了提高代码的可读性,您可以签出并尝试释放多个continue语句:

    switch(usrAns) {
        case 1:
            if(check(Posx, Posy, maze, movx[1], movy[1])){
                System.out.println("Cannot move past cave boundary! Try something else.\n");
            }
            else{
                Posy = Posy - 1;
                System.out.printf("This is Posx %d and Posy %d\n", Posx, Posy);
            }
            break;
        case 2:
        ...
        ...
        default:
            while (usrAns >= 5 || usrAns < 0){
                System.out.println("Please enter a number between 0 and 4:\n");
                usrAns = sc2.nextInt();
                if (usrAns == 0){
                    System.out.println("Bye!\n");
                    found = false;
                    break;
                }
            }
            break;
         }
开关(usrAns){
案例1:
如果(检查(Posx,Posy,maze,movx[1],movy[1])){
System.out.println(“无法移动超过洞穴边界!请尝试其他操作。\n”);
}
否则{
Posy=Posy-1;
System.out.printf(“这是Posx%d和Posy%d\n”,Posx,Posy);
}
打破
案例2:
...
...
违约:
而(usrAns>=5 | | usrAns<0){
System.out.println(“请输入一个介于0和4之间的数字:\n”);
usrAns=sc2.nextInt();
如果(usrAns==0){
System.out.println(“再见!\n”);
发现=错误;
打破
}
}
打破
}

不仅我们不需要在循环内初始化Posx和Posy,我们也不想这样做,因为每次循环转动时它都会重置我们的位置。