簡體   English   中英

無法將值正確地從一種方法傳遞到另一種方法,在Java中為零

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

我有一個迷宮類,我需要在其中找到起點和終點,雖然不是我現在正在運行的方式,但它不是硬編碼的。 我有四個班級可以在迷宮中找到起點和終點的點。 我的問題是,每當我將方法傳遞過來時,我的findX()和findY()類都將返回0。這對於我需要更改的內容似乎很簡單,但是我很生銹。 當坐標是硬編碼時,代碼運行得很好,但是我將需要使用不同的迷宮,因此我需要返回點。

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)); 

我知道您為什么只得到零。 在所有方法中,您都必須為變量分配一個值。 例如,在function findX ,您必須分配xa的i值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM