While 循环陷入无限循环

发布于 2024-12-05 01:54:50 字数 1773 浏览 0 评论 0原文

我试图找出为什么我的程序重复打印相同的语句。当我输入良好的值时,我能够使此方法正常工作,但我设置了一个 else 语句来捕获无效的选择。当我的 else 语句运行时,它会无限打印,而 while 循环永远不会开始。我想不通。我将粘贴整个方法,但将问题区域加粗。我希望这一点很清楚。

public static int[][] placeCheese(int [][] gameBoard, String Player1Name) 
{ 
    Scanner console = new Scanner(System.in);
    int turnTaker = 0;
    int validRow = 0;
    int validCol = 0;
    int RowIndex = -1;
    int ColIndex = -1;
    while(turnTaker == 0)
    {
        while(validRow == 0)
        {
            System.out.println( Player1Name + ", please place your cheese by choosing a row, or choose -1 to exit.");
            RowIndex = console.nextInt() -1; 
            if(RowIndex < gameBoard.length && RowIndex >=0)
            {
                validRow++;
            }
            else if(RowIndex == -2)
            {
                System.out.println("Thanks for playing, " + Player1Name + " has forfeited!");
                System.exit(0);
            }
        }
        while(validCol == 0){
            System.out.println( Player1Name + ", please place your cheese by choosing a column, or choose -1 to exit.");
            ColIndex = console.nextInt() -1; 
            if(ColIndex < gameBoard.length && ColIndex >=0)
            {
                validCol++;
            }
            else if(RowIndex == -2)
            {
                System.out.println("Thanks for playing, " + Player1Name + " has forfeited!");
                System.exit(0);
            }
        }
    if(gameBoard[RowIndex][ColIndex] == 0)
    {
        gameBoard[RowIndex][ColIndex] = 1;
        turnTaker++;
        numPieces1--;
    }
    else 
    {
        System.out.println("this space is already occupied, please choose again.");
    }
    return gameBoard;
}

I am trying to figure out why my program is printing the same statement repeatedly. I am able to get this method to work fine when I am putting in good values, but I have an else statement set up to catch invalid selections. When my else statement runs, it prints infinitely and the while loop never commences. I can't figure it out. I am going to paste the entire method, but bold the problem area. I hope this is clear.

public static int[][] placeCheese(int [][] gameBoard, String Player1Name) 
{ 
    Scanner console = new Scanner(System.in);
    int turnTaker = 0;
    int validRow = 0;
    int validCol = 0;
    int RowIndex = -1;
    int ColIndex = -1;
    while(turnTaker == 0)
    {
        while(validRow == 0)
        {
            System.out.println( Player1Name + ", please place your cheese by choosing a row, or choose -1 to exit.");
            RowIndex = console.nextInt() -1; 
            if(RowIndex < gameBoard.length && RowIndex >=0)
            {
                validRow++;
            }
            else if(RowIndex == -2)
            {
                System.out.println("Thanks for playing, " + Player1Name + " has forfeited!");
                System.exit(0);
            }
        }
        while(validCol == 0){
            System.out.println( Player1Name + ", please place your cheese by choosing a column, or choose -1 to exit.");
            ColIndex = console.nextInt() -1; 
            if(ColIndex < gameBoard.length && ColIndex >=0)
            {
                validCol++;
            }
            else if(RowIndex == -2)
            {
                System.out.println("Thanks for playing, " + Player1Name + " has forfeited!");
                System.exit(0);
            }
        }
    if(gameBoard[RowIndex][ColIndex] == 0)
    {
        gameBoard[RowIndex][ColIndex] = 1;
        turnTaker++;
        numPieces1--;
    }
    else 
    {
        System.out.println("this space is already occupied, please choose again.");
    }
    return gameBoard;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

琴流音 2024-12-12 01:54:50

第一次调用时,它将强制您提供有效的列号和行号;方块将被设置,turnTaker 将递增,循环将终止。

第二次调用此函数时,如果选择的行号和列号相同,则 turnTaker不会递增,因为数组中所选的位置不为 0。 validRowvalidCol 不是 0,此外,它永远不会要求您提供更多数字 - 它只会进入无限循环打印消息而不再提示!

打印消息的“else”子句可以通过再次将 validRowvalidCol 设置为 0 来解决此问题。正如其他人指出的那样,如果这些变量和 TurnTaker 也是布尔值而不是整数,那就更好了。

The first time this is called, it will force you to provide valid column and row numbers; the square will get set, turnTaker will be incremented, and the loop will terminate.

The second time this is called, if the row and column numbers chosen are the same, then turnTaker will not be incremented because the chosen spot in the array is not 0. Since validRow and validCol aren't 0, furthermore, it will never ask you for more numbers -- it will just go into an endless loop printing the message without ever prompting again!

Your "else" clause that prints the message could fix this by setting validRow and validCol to 0 again. As someone else has pointed out, it would be much better if these variables and turnTaker as well were booleans, not ints.

要走就滚别墨迹 2024-12-12 01:54:50

您的代码难以辨认,但如果我不得不猜测,我会说您没有在 else 语句中更改turnTaker。无限循环!

Your code is illegible but if I had to guess I'd say you weren't changing turnTaker in your else statement. Infinite loop!

酒中人 2024-12-12 01:54:50

您不会更改turnTaker 的值,因此它将始终保持为零。

You're not changing the value of turnTaker, so it will always stay zero.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文