[英]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.