简体   繁体   中英

Not passing value correctly from one method to another, getting zero in Java

I have a maze class where I need to find the beginning and end, it is not hard coded, though that is how I am running my code now. I have four classes to find the points on the maze where it starts and ends. My problem is whenever I pass my method over, my findX() and findY() classes return 0. This seems so simple as to what I need to change but I am rusty. The code runs great when the coordinates are hard coded, but I will need to use different mazes, hence I need to return the points.

public static int findX () {
    for ( int i = 0; i < maze.length; i++ ) {
        for ( int j = 0; j < maze[i].length; j++ ) {
            if ( maze[i][j] == p ) {
                System.out.println("New Point i: " + i);
            }
        }
    }
    return x;
}

//Find the y value for the start
public static int findY (int y) {
    for ( int i = 0; i < maze.length; i++ ) {
        for ( int j = 0; j < maze[i].length; j++ ) {
            if ( maze[i][j] == p ) {
                System.out.println("New Point j: " + j);
            }
        }
    }
    return y;
}

//Find the x value for the end
public int findXend (int x) {
    for ( int i = 0; i < maze.length; i++ ) {
        for ( int j = 0; j < maze[i].length; j++ ) {
            if ( maze[i][j] == e ) {
                System.out.println("Last Point i: " + i);
            }
        }
    }
    return x;
}

//Find the y value for the end
public int findYend (int y) {
    for ( int i = 0; i < maze.length; i++ ) {
        for ( int j = 0; j < maze[i].length; j++ ) {
            if ( maze[i][j] == e ) {
                System.out.println("Last Point j: " + j);
            }
        }
    }
    return y;
}

public static void solveStack() {

    System.out.println("x:" + findX() + ", y:" + findY(y)); 

    //save the maze
    //char[][] savedMaze = clone(); 

    //declare the locations stack 
    Stack<MazePos> candidates = new Stack<MazePos>(); 

    //insert the start 
    candidates.push(new MazePos(START_I, START_J)); 

I know why you are getting only zeros. In all of your methods you have to assign a value to your varible. For example in function findX , you have to assign xa value of i.

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