简体   繁体   English

无法将值正确地从一种方法传递到另一种方法,在Java中为零

[英]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. 我的问题是,每当我将方法传递过来时,我的findX()和findY()类都将返回0。这对于我需要更改的内容似乎很简单,但是我很生锈。 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. 例如,在function findX ,您必须分配xa的i值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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