Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过使用二维阵列创建tic-tac趾板_Java_Arrays_Multidimensional Array_Tic Tac Toe - Fatal编程技术网

Java 通过使用二维阵列创建tic-tac趾板

Java 通过使用二维阵列创建tic-tac趾板,java,arrays,multidimensional-array,tic-tac-toe,Java,Arrays,Multidimensional Array,Tic Tac Toe,在这段代码中,我尝试创建一个二维数组来表示tic-tac趾板(带有用户输入),但无论我在“TicTacLine”中输入什么,程序总是会显示“你以前从未玩过tic-tac-toe吗?如果你没有玩过也可以,但仅供参考,它会玩x和o。”,如果uno、dos和TRE中的字符不是x或o,这就是我写的消息 public class TicTacToe { public static void main(String[] args) { int TicTac[][]= new int

在这段代码中,我尝试创建一个二维数组来表示tic-tac趾板(带有用户输入),但无论我在“TicTacLine”中输入什么,程序总是会显示“你以前从未玩过tic-tac-toe吗?如果你没有玩过也可以,但仅供参考,它会玩x和o。”,如果uno、dos和TRE中的字符不是x或o,这就是我写的消息

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);
            } 
          } 
        }
    }
公共类TicTacToe{
公共静态void main(字符串[]args){
int TicTac[][]=新int[3][3];
System.out.println(“输入您想要看到的Tic-Tac趾板,一次一行”);
扫描仪=新的扫描仪(System.in);
字符串TicTacLine=scanner.nextLine();
int循环=0;
if(TicTacLine.length()3){//我试图通过while循环中的一系列输入来定义数组。
System.out.println(“Tic-tac-toe在3×3的网格中播放。这意味着如果你想输入一行,你就要输入3个字符,不多也不少。”);
}否则{
而(loop!=3){//我们计算循环,这样就只有3条不同的线了
char uno=TicTacLine.charAt(0);
char dos=TicTacLine.charAt(1);
char tres=TicTacLine.charAt(2);
如果(uno!=“x”| uno!=“o”| dos!=“x”| dos!=“o”| tres!=“x”| tres!=“o”){
System.out.println(“你以前从未玩过井字游戏吗?如果你没有玩过也没关系,但仅供参考,它会玩x和o的游戏”);
打破
}否则{
如果(循环==0){
TicTac[0][0]=uno;
TicTac[0][1]=dos;
TicTac[0][2]=tres;
loop=++loop;
TicTacLine=scanner.nextLine();
}if(循环==1){
TicTac[1][0]=联合国办事处;
TicTac[1][1]=dos;
TicTac[1][2]=tres;
loop=++loop;
TicTacLine=scanner.nextLine();
}如果(循环==2){
TicTac[2][0]=联合国办事处;
TicTac[2][1]=dos;
TicTac[2][2]=tres;
loop=++loop;
TicTacLine=scanner.nextLine();
}
}
}
}   
如果(循环==3){
对于(int[]行:TicTac){
打印行(row);
} 
} 
}
}

首先,在使用条件语句进行结构化时,我想给大家一个重要的建议。尽量减少if或else语句中的代码

其次是错误

 if (uno != 'x' | uno != 'o' | dos != 'x' | dos != 'o' | tres != 'x' | tres != 'o')
您可以看到对布尔语句使用了|运算符。您显然混淆了位运算符|和逻辑运算符| |(or)

了解两者的区别是非常重要的。您可以看到,按位运算符基本上接受两个二进制数 像

因为它类似于逻辑运算符,但适用于1和0。 注意:还有一个按位AND运算符&
但这并不是逻辑上的&

首先,当涉及到使用条件语句进行结构时,我想给你一个重要的建议。尽量减少if或else语句中的代码

其次是错误

 if (uno != 'x' | uno != 'o' | dos != 'x' | dos != 'o' | tres != 'x' | tres != 'o')
您可以看到对布尔语句使用了|运算符。您显然混淆了位运算符|和逻辑运算符| |(or)

了解两者的区别是非常重要的。您可以看到,按位运算符基本上接受两个二进制数 像

因为它类似于逻辑运算符,但适用于1和0。 注意:还有一个按位AND运算符&
但它不是逻辑&

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

if (uno != 'x' | uno != 'o' | dos != 'x' | dos != 'o' | tres != 'x' | tres != 'o')
为什么?? 因为您正在将所有六个子表达式的结果合并在一起。因为在这种情况下,其中至少有三个始终为真,并且可能所有六个都为真,那么整个表达式将始终为真

看看前两个按位ORing的布尔表达式。由于
uno!='x'
为真,或
uno!='o'
为true,或者两者都为true,因此表达式将始终为true,因此您将始终执行

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不是'x'而uno不是'o'
如果dos不是'x'而dos不是'o',
如果
tres不是'x'而tres不是'o',则求值为真


有关Java运算符的更多信息,Oracle在此处提供了很好的文档:

您在
IF
语句中的布尔表达式将始终为true:

if (uno != 'x' | uno != 'o' | dos != 'x' | dos != 'o' | tres != 'x' | tres != 'o')
为什么?? 因为您正在将所有六个子表达式的结果合并在一起。因为在这种情况下,其中至少有三个始终为真,并且可能所有六个都为真,那么整个表达式将始终为真

看看前两个按位ORing的布尔表达式。由于
uno!='x'
为真,或
uno!='o'
为true,或者两者都为true,因此表达式将始终为true,因此您将始终执行

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不是'x'而uno不是'o'
如果dos不是'x'而dos不是'o',
如果
tres不是'x'而tres不是'o',则求值为真

关于Java操作符的更多信息,Oracle有很多好的做法
ticTacLine = scanner.nextLine();
String ticTacLine = scanner.nextLine();