Java 有没有办法检查数组中的特定点是否具有特定字符?

Java 有没有办法检查数组中的特定点是否具有特定字符?,java,Java,我想检查电路板的某些区域是否具有特定的图层值 public boolean diagonal( char[][] theBoard, char thePlayer) { int x = 0; while (x <theBoard.length){ if (thePlayer == theBoard[x][x]) { x++; } else { return false; } } pu

我想检查电路板的某些区域是否具有特定的图层值

public boolean diagonal( char[][] theBoard, char thePlayer) {
    int x = 0;

    while (x <theBoard.length){
      if (thePlayer == theBoard[x][x]) {
        x++;
      }
      else {
        return false;
      }
    }
public boolean diagonal(char[]theBoard,char thePlayer){
int x=0;

(x由于您使用的是多维数组,请尝试使用嵌套for循环

public boolean diagonal(char[ ][ ] theBoard, char thePlayer) {
    boolean result = false;
    int x, y; // These will tell you the coordinates of thePlayer if found on theBoard

    for(int i = 0; I < theBoard.length; i++)
        for(int j = 0; j < theBoard[i].length; j++)
            if (thePlayer == theBoard[i][j]) {
                result = true;
                // Add if you want to know coordinates of the player
                x = i;
                y = j;
           }

    return result;
}
public boolean diagonal(char[ ][ ] theBoard, char thePlayer) {
    boolean result = false;
    int x; // This will tell you the coordinates of thePlayer if found on theBoard

    for(int i = 0; I < theBoard.length; i++)
        if (thePlayer == theBoard[i][i]) {
            result = true;
            // Add if you want to know coordinates of the player
            x = i;
       }

    return result;
}
如果要检查该层是否仅在数组的对角索引中,可以使用单个for循环

public boolean diagonal(char[ ][ ] theBoard, char thePlayer) {
    boolean result = false;
    int x, y; // These will tell you the coordinates of thePlayer if found on theBoard

    for(int i = 0; I < theBoard.length; i++)
        for(int j = 0; j < theBoard[i].length; j++)
            if (thePlayer == theBoard[i][j]) {
                result = true;
                // Add if you want to know coordinates of the player
                x = i;
                y = j;
           }

    return result;
}
public boolean diagonal(char[ ][ ] theBoard, char thePlayer) {
    boolean result = false;
    int x; // This will tell you the coordinates of thePlayer if found on theBoard

    for(int i = 0; I < theBoard.length; i++)
        if (thePlayer == theBoard[i][i]) {
            result = true;
            // Add if you want to know coordinates of the player
            x = i;
       }

    return result;
}
public boolean diagonal(char[]theBoard,char thePlayer){
布尔结果=假;
int x;//如果在电路板上找到,这将告诉您图层的坐标
for(int i=0;i
在我看来,对这样的事情使用for循环更容易。while循环本质上是相同的事情,但实现方式不同,但在这种情况下,for循环看起来更干净,更容易遵循。最后,由您决定哪一个更容易实现

      for(char[] firstarray  : theBoard){
           for(char ch : firstarray){
         boolean matches = (thePlayer == ch) ? true : false;
         if(matches){
         System.out.println("matchFound");        
         }else{ 
         System.out.println("no match");
         }

         }
         }
因为你有一个二维数组,你需要循环数组两次。你可以使用普通的for循环bt,使用增强的for循环会容易得多。

公共布尔对角(char[][]theBoard,char thePlayer){
public boolean diagonal( char[][] theBoard, char thePlayer) { 
        for(int i = 0; I < theBoard.length; i++)
                for(int j = 0; j < theBoard[i].length; j++)
                        if (thePlayer == theBoard[i][j]) {
                                return = true;
                        }
        return false;
}
for(int i=0;i
你的循环不一定会完成。@宪兵为什么不呢?我甚至不认为这段代码会编译。可能在所有情况下都不会有返回语句。@TimBiegeleisen是正确的。如果电路板的对角线上到处都有
图层
,那么它将在没有返回值的情况下退出循环。现在还不清楚它的目标是什么e asker是,也不知道他们是如何通过他们的思维过程达到这个代码的。你正在检查对角线上是否填充了
thepayer
。如果不是,你将返回
false
。如果这是你想要的,那就好了-除了在while循环后需要返回
true
这个事实。只需添加它。