如何让计数器在while循环中工作?(How can I get a counter to work in a while loop?)

编程入门 行业动态 更新时间:2024-10-08 06:28:58
如何让计数器在while循环中工作?(How can I get a counter to work in a while loop?)

所以我试着让我们说int Posx; 保持一个数字然后当用户浏览菜单时,根据他们选择的内容,将向int Posx添加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);

任何想法,提示或技巧将不胜感激。

so I'm trying to have lets say int Posx; hold a number and then when a user navigates through a menu, depending on what they chose, will either add 1, 0 or -1 to int Posx. I can't get Posx to keep the number after it added or subtracted. I'm trying to navigate through an array and I want to keep 2 ints as markers to let me know where I currently am in the array in regards to x and y.

tl;dr How can I keep a counter inside a do/while loop?

Here is where the problem is:

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

Any ideas, tips, or tricks would be greatly appreciated.

最满意答案

我认为问题在于这两条线

//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语句并尝试删除多个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; }

I think the problem lies in these two lines

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

You are resetting Posx and Posy every iteration. Try setting them outside your while loop. Like this (you don't want to initialize them over and over again). Same for movx and 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++) { ... ...

To improve readability of your code you could check out the switch statement and try to loose the multiple continue statements:

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; }

更多推荐

本文发布于:2023-08-04 03:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405388.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:计数器   工作   counter   work   loop

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!