gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:48:50 24 4
gpt4 key购买 nike

所以我试着让 int Posx; 保存一个数字,然后当用户浏览菜单时,根据他们的选择,将添加 1、0 或 -1到 int Posx。我无法让 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);

如有任何想法、提示或技巧,我们将不胜感激。

最佳答案

我觉得问题出在这两行

    //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++) {
...
...

为了提高代码的可读性,您可以查看 switch statement并尝试松开多个 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;
}

关于java - 如何让计数器在 while 循环中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19148605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com