简体   繁体   中英

Java 2D Array Search Not Working

This program is supposed to return the location of a piece on a board.The board is being represented as a 2D array. The loop simply cycles through the columns and then if the piece is still not found it goes to the next row. However,only the [-1,-1] array is being returned( no pieces are being found). I have not been able to find the error on my own, so any help

public int[] pieceFinder(int piece) {

    int[][] board={{43,44,45,50,47,48,49},
                   {36,37,38,39,40,41,42},
                   {29,30,31,32,33,34,35},
                   {22,23,24,25,26,27,28},
                   {15,16,17,18,19,20,21},
                   {8,9,10,11,12,13,14},
                   {1,2,3,50,5,6,7}};    

    int row=0;  
    boolean found=false;
    int[] location={-1,-1} ;  

    for(int column=0;found==true;column++) {
        if(board[row][column]==piece) {
            found=true ;
            location[0]=row; 
            location[1]=column;
        }

        else if(column==6) {
            if(row==6) { 
                break;
            }

        else {
            row++;
        }
    } 
    return location;
}             
boolean found=false;
for(int column=0;found==true;column++){

As you can see found is false. And for loop checks for true value for found. It will never enter the loop. That's why locations gets returned as it is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM