嗨,大家好,我正在研究一个大学课程的程序,该程序使用名为get_line()的方法来递归计算从网格上的一个点到另一个点的连续位置列表。当我运行它时,我在方法中最后一个return语句的行处发生堆栈溢出。我想知道是否有其他人可以查看方法,看看有没有什么看起来完全错误。该方法提供如下:java stackoverflow错误
谢谢你的帮助!
位置是包含行r和列c的对象。
private Vector<location> get_line(location from, location to) {
location nextLoc = new location();
Vector<location> loc = new Vector<location>();
Random r = new Random();
if(to.r == from.r && to.c == from.c) {
return(loc);
} else {
if(to.r > from.r && to.c > from.c) {
nextLoc.r = from.r + 1;
nextLoc.c = from.c + 1;
} else if(to.r < from.r && to.c < from.c) {
nextLoc.r = from.r - 1;
nextLoc.c = from.c - 1;
} else if(to.r < from.r && to.c > from.c) {
nextLoc.r = from.r - 1;
nextLoc.c = from.c + 1;
} else if(to.r > from.r && to.c < from.c) {
nextLoc.r = from.r + 1;
nextLoc.c = from.c - 1;
} else if(to.r == from.r && to.c > from.c) {
if(r.nextInt(2) == 0) {
nextLoc.r = from.r + 1;
} else {
nextLoc.r = from.r - 1;
}
nextLoc.c = from.c + 1;
} else if(to.r == from.r && to.c < from.c) {
if(r.nextInt(2) == 0) {
nextLoc.r = from.r + 1;
} else {
nextLoc.r = from.r - 1;
}
nextLoc.c = from.c - 1;
} else if(to.r < from.r && to.c == from.c) {
nextLoc.r = from.r - 1;
if(r.nextInt(2) == 0) {
nextLoc.c = from.c + 1;
} else {
nextLoc.c = from.c - 1;
}
} else if(to.r > from.r && to.c == from.c) {
nextLoc.r = from.r + 1;
if(r.nextInt(2) == 0) {
nextLoc.c = from.c + 1;
} else {
nextLoc.c = from.c - 1;
}
}
loc.add(nextLoc);
return(get_line(nextLoc,to)); //stack overflow error occurs here.
}
}
你应该重申这一点,并添加作业标签,只是为了更多的预见。 – 2009-11-18 17:37:19
在Java中习惯使用以高级字符开头的所有类名; “我的位置”看起来更像是一个变量,而不是一个班级,直到我遇到心理错位并回去仔细检查。永远不要低估会议的力量! – 2009-11-18 17:53:38
我不知道有一个家庭作业标签,这对那些试图回答编程问题的人有帮助吗?此外,我的问题中的第一句话是“嗨,伙计们,我正在为**大学课程**计划” – seventeen 2009-12-02 17:34:12