【问题标题】:Creating a tic tac toe board through the use of a two dimensional array通过使用二维数组创建井字棋盘
【发布时间】:2018-03-26 13:01:08
【问题描述】:

在这段代码中,我尝试创建一个代表井字游戏的二维数组(使用用户输入),但无论我在“TicTacLine”中输入什么内容,程序总是会弹出“你从未玩过井字游戏” Toe before ?如果你没有也可以,但仅供参考,它与 x's 和 o's 一起使用。 >

public class TicTacToe {

    public static void main(String[] args) {
        int TicTac[][]= new int[3][3];

        System.out.println("Enter the Tic Tac Toe board you want to see, one line at a time.");
        Scanner scanner = new Scanner(System.in);
        String TicTacLine = scanner.nextLine();     
        int loop = 0;

        if (TicTacLine.length()<3 | TicTacLine.length()>3) {  // I try to define the array by a series of inputs that go in the while loop.
            System.out.println("Tic-tac-toe plays in a 3×3 grid. This means if you want to input a line, you would want to input 3 characters, no more, no less.");
        } else { 
            while (loop != 3) { // we count the loops so that there's only 3 different lines
                char uno = TicTacLine.charAt(0);
                char dos = TicTacLine.charAt(1);
                char tres = TicTacLine.charAt(2);
                if (uno != 'x' | uno != 'o' | dos != 'x' | dos != 'o' | tres != 'x' | tres != 'o') {
                    System.out.println("Have you never played Tic Tac Toe before ? It's okay if you haven't, but just FYI, it plays with x's and o's.");
                    break;
                } else { 
                    if (loop == 0) {
                        TicTac[0][0] = uno;
                        TicTac[0][1] = dos;
                        TicTac[0][2] = tres; 
                        loop = ++loop;
                        TicTacLine = scanner.nextLine();
                    } if (loop == 1) {
                        TicTac[1][0] = uno;
                        TicTac[1][1] = dos;
                        TicTac[1][2] = tres;
                        loop = ++loop;
                        TicTacLine = scanner.nextLine();
                    } if (loop == 2) {
                        TicTac[2][0] = uno;
                        TicTac[2][1] = dos;
                        TicTac[2][2] = tres;
                        loop = ++loop;
                        TicTacLine = scanner.nextLine();
                        }
                    }
                }
            }   
        if (loop == 3) {
        for(int[] row : TicTac) {
            PrintingRow(row);
            } 
          } 
        }
    }

【问题讨论】:

  • 确保使用 .equals 而不是 == 来比较字符串。此外,您正在使用 |而不是 ||为“或”。此外,在“TicTacLine.length()3”行中,您可以改用“TicTacLine.length() != 3”。此外,只是你应该使用的 java 中的一个约定:你在 java 中命名了一个变量“TicTacLine”,我们使用 lowerCamelCase 所以它应该重命名为:“ticTacLine”你的类名,“TicTacToe”很好,类名使用 PascalCase .编辑:您正在以正确的方式比较字符。我以为那些是字符串。
  • 想一想:uno 的什么值会使uno != 'x' | uno != 'o' 成为false?它必须是&amp;&amp;
  • 我确实更改了我的变量名称和 ticTacLine.length() != 3 但它不会使错误消息消失。

标签: java arrays multidimensional-array tic-tac-toe


【解决方案1】:

首先,在使用条件语句进行结构化时,我想给你一个重要的建议。尝试在 if 或 else 语句中最小化您的代码

其次是错误

 if (uno != 'x' | uno != 'o' | dos != 'x' | dos != 'o' | tres != 'x' | tres != 'o')

你看到你使用 |布尔语句的运算符。您显然混淆了按位运算符 |和逻辑运算符 || (或)

了解其中的区别非常重要。您会看到按位运算符基本上需要两个二进制数 喜欢

1001 | 1010 = 1011 

因为它类似于逻辑运算符,但用于 1 和 0。 注意:还有看起来像 & 的按位 AND 运算符 但它不是合乎逻辑的 &&

【讨论】:

  • 虽然正如您所指出的,在这种情况下使用按位运算符是一个错误,但错误的根本原因是表达式总是计算为真,因为 uno 要么不是'x' 或不是 'o'。只有当两个条件都成立时,代码才应该评估为真,因此这两个之间需要一个逻辑与(并且在其他两个字符的另一个测试中也是如此)。
  • 感谢我看到运营商的评论,我只是认为这是错误的根源。感谢您的解释,希望我能从中吸取教训。
【解决方案2】:

IF 语句中的布尔表达式将始终为真:

if (uno != 'x' | uno != 'o' | dos != 'x' | dos != 'o' | tres != 'x' | tres != 'o')

为什么? 因为您将所有六个子表达式的结果组合在一起。由于其中至少三个始终为真,并且可能所有六个都为真,因此整个表达式将始终为真。

查看前两个布尔表达式,它们是按位或运算在一起的。这总是返回真,因为uno != 'x' 为真,或uno != 'o' 为真,或两者兼而有之,因此表达式将始终为真,因此您将始终执行

System.out.println("Have you never played Tic Tac Toe before ? It's okay if you haven't, but just FYI, it plays with x's and o's.");
break;

您需要使用 逻辑 OR 和 AND 将其重写如下:

if ((uno != 'x' && uno != 'o') || (dos != 'x' && dos != 'o') || (tres != 'x' && tres != 'o')) {

这就是说,如果uno is not 'x' and uno is not 'o'or 评估为真if dos is not 'x' and dos is not 'o'or 如果tres is not 'x' and tres is not 'o' 评估为真,则评估为true

有关 Java 运算符的更多信息,Oracle 在这里有很好的文档:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

【讨论】:

    【解决方案3】:

    一些一般的事情:

    首先你不应该这样做

    loop = ++loop;
    

    如果你想增加loop 只需代码

    ++loop;
    

    第二个,而不是做所有这些if else (loop == ...) 可以做的事情

    TicTac[loop][0] = uno;
    TicTac[loop][1] = dos;
    TicTac[loop][2] = tres;
    ++loop;
    ticTacLine = scanner.nextLine();
    

    只有一次。

    最后,如果您知道循环的数量,那么使用 for 循环而不是 while 循环会更容易阅读。

    现在到程序本身。

    ticTacLine = scanner.nextLine();
    

    不需要,因为你正在做

    String ticTacLine = scanner.nextLine();
    

    在循环的每个开始。目前,您在每个循环中读取两次输入。

    并且打印应该在循环之外。然后你也不需要检查循环值,只需调用一次。

    你从 inout 中读取的是字符,但是你放入数组的是字符的 int 值。如果要存储字符,则必须使用字符数组。

    【讨论】:

    • 感谢您的回答,但不幸的是,这并不能解决我遇到的输入数量问题、并不总是出现的错误消息以及显示数字的板。
    • 我分两步写了我的答案。请再检查一次。发布答案后,我想到的一个想法是您应该尝试调试代码。这对于找出两次扫描和 char 到 int 转换的问题非常有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    相关资源
    最近更新 更多