Try and catch for InputMismatchException handling. I want to create TicTacToe game, that can be played with console

huangapple go评论65阅读模式
英文:

Try and catch for InputMismatchException handling. I want to create TicTacToe game, that can be played with console

问题

在这里找到了如何正确处理“InputMismatchException”的解决方案,并将其实现到我的代码中。但结果仍然不符合我的预期。

我需要扫描两个整数(x、y坐标)并将它们添加到数组中。尝试为x和y分别创建两个循环,但结果是相同的。

private static void enterCoordinate(char[][] layoutMatrix) {
    int[] xy = new int[2];
    boolean again = true;
    while (again) {
        try {
            System.out.print("输入坐标:");
            for (int i = 0; i < 2; i++) {
                xy[i] = scanner.nextInt();
            }
            again = false;
        } catch (InputMismatchException e) {
            System.out.println();
            System.err.println("您应该输入数字!");
            scanner.next();
        }
    }
}

控制台输出如下:

Try and catch for InputMismatchException handling. I want to create TicTacToe game, that can be played with console

这不是我想要看到的结果。

英文:

Found a solution in here how to properly handle "InputMismatchException" and implemented it to my code. But the result is still not what I expected.

I have to scan for 2 integers (x, y coordinates) and add them to array. Tried to create two loops for x and y separately but, result is the same.

    private static void enterCoordinate(char[][] layoutMatrix) {
        int[] xy = new int[2];
        boolean again = true;
        while (again) {
            try {
                System.out.print(&quot;Enter the coordinates: &quot;);
                for (int i = 0; i &lt; 2; i++) {
                    xy[i] = scanner.nextInt();
                }
                again = false;
            }
            catch (InputMismatchException e) {
                System.out.println();
                System.err.println(&quot;You should enter numbers!&quot;);
                scanner.next();
            }
        }

output is like below

Try and catch for InputMismatchException handling. I want to create TicTacToe game, that can be played with console

This is not that result what I want to see

答案1

得分: 7

如果 nextInt() 失败,它不会消耗失败的标记... 所以你会进行递归,立即再次调用 nextInt(),然后那次调用也会失败... 所以你会再次递归,以此类推,永远地进行下去。

在失败时,你应该调用 scanner.next() 来消耗那个“不是数字的标记”。只是添加这行代码可能会修复问题,但我建议你同时将其转换为while循环... 我不认为在这里进行递归会有任何好处(而且如果用户输入了很多无效的数字,仍然可能导致堆栈溢出)。

英文:

If nextInt() fails, it doesn't consume the failed token... so you'll recurse, immediately call nextInt() again, and that will fail too... so you'll recurse again, etc, forever.

You should probably call scanner.next() on failure, to consume the "token that wasn't a number". Just adding that line would probably fix the problem, but I'd suggest you also convert this into a while loop instead... I don't see any benefit in recursing here (and you could still end up with a stack overflow if the user enters lots of invalid numbers).

huangapple
  • 本文由 发表于 2020年8月22日 16:52:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63534309.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定