2017-10-14 120 views
0

在此代码中,我尝试创建一个代表tic tac脚趾板(带用户输入)的二维数组,但无论我在“TicTacLine “,该节目总是提出”你以前从来没有玩过井字趾吗?没关系,如果你没有,但只是供参考,它使用x和o“。这是我写的信息,如果字符在uno, dos和tres不是x或o的。通过使用二维数组创建一个tic tac脚趾板

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); 
      } 
      } 
     } 
    } 
+0

请确保您使用.equals而不是==来比较字符串。另外,您正在使用|而不是||为“或”。此外,在“TicTacLine.length()<3 | TicTacLine.length()> 3”行中,您可以改为使用“TicTacLine.length()!= 3”。另外,在java中你应该使用一个约定:你在java中命名了一个变量“TicTacLine”,我们使用lowerCamelCase,所以它应该重命名为:“ticTacLine”你的类名“TicTacToe”很好,类名使用PascalCase 。编辑:你正在比较字符的正确方法。我认为这些是一秒钟的字符串。 – retodaredevil

+2

想一想:'uno'的价值会让'uno!='x'| uno!='o''是'false'?它需要是&&'。 – 4castle

+0

我确实改变了我的变量和ticTacLine.length()!= 3的名字,但它并没有让错误消息消失。 – Dracose

回答

2

你的布尔表达式将永远是正确的:

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和与运算如下:

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

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

有关Java运算符的更多信息,甲骨文公司拥有良好的文档在这里:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

0

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

第二继承人的错误

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

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

了解其中的差异非常重要。你看,位运算符基本上采用两个二进制数 像

1001 | 1010 = 1011 

,因为它像逻辑运算符,但1和为零。 注意:也Theres按位与运算,看起来像& 但它不是IF语句中的逻辑& &

+0

尽管在您指出的情况下使用按位运算符是一个错误,但错误的根本原因是该表达式总是计算为true,因为uno不是'x'或不是' O”。如果两个条件都成立,则代码只应计算为真,因此这两者之间需要逻辑AND(另一个测试用于其他两个字符)。 – ScottK

+0

感谢您的评论,我看到了操作员,我只是认为这是错误的根源。我感谢你的解释,并希望我能从中学习。 –

1

一些一般的东西:

首先,如果你想增加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值。如果你想存储字符,你必须使用字符数组。

+0

谢谢你的回答,但是这并不能很好地解决我在输入数量,没有经常出现的错误信息和显示数字的板子时遇到的问题。 – Dracose

+1

我分两步写了我的答案。请再检查一次。在发布我的答案后,我想到的一点是你应该尝试调试代码。这对于找出两个扫描和char到int casting的问题非常有用。 –