[英]Abit stuck why i cant get this working :/
public class Robot
{
public static void main(String []args){
ArrayList<String> list = new ArrayList<String>();
String path = "";
int N = 2;
int ol1_right = 1;
int ol1_down = 1;
int ol2_right = 1;
int ol2_down = 1;
buildPath (right, down, path, allPaths, N);
System.out.println (allPath(grid));
}
public void BuildPath (int right, int down, String path,
ArrayList<String> allPaths, int N){
path += "(" + down "," + right + ")->";
if (right == N-1 && down == N-1){
allPaths.add(path);
return;
}
else if (right +1 == ol1_right && down == ol1_down) && (right +1 ==
ol2_right && down == ol2_down);{
return;
}
if (right +1 == ol1_right && down == N-1) || (right +1 == ol2_right
&& down == N-1);{
return;
}
if (right == N-1 && down +1 == ol1_down) || (right == N-1 && down +1
== ol2_right);{
return;
}
if (right == N-1 || right +1 == ol1_right && down == N-1 ||
right +1 == ol2_right && down == N-1){
BuildPath (right, down + 1, path, allPaths, N);
}
else if (down == N-1 || right +1 == ol1_right && down == ol1_down && right +1 ==
ol2_right && down == ol2_down ){
BuildPath (right + 1, down, path, allPaths, N);
}
else
BuildPath (right + 1, down, path, allPaths, N);
BuildPath (right, down + 1, path, allPaths, N);
}
}
对不起,我对这种事情还很陌生,我一直试图使该程序正常工作,但是对下一步工作却束手无策。 我收到诸如“表达式的非法开头”(使用||和&&时抛出)或“不是语句”之类的错误(路径+ =“(” +下“,” +右+“)->”;)所有im尝试做的是让(机器人)沿着网格移动,同时仅左右移动,但也可以将网格的某些部分标记为超限。 任何帮助将不胜感激,我知道这一切看起来都很糟糕,但是只是想摆脱这个陷阱,所以对任何菜鸟错误或愚蠢错误仍然很抱歉:)
有很多问题,对于Main()
初学者来说,
int ol1_right = 1;
int ol1_down = 1;
int ol2_right = 1;
int ol2_down = 1;
您声明并初始化它,然后尝试从BuildPath()
访问而不将其作为参数传递。 您不能这样做,因为在Java中,变量的范围仅限于定义它的块,除非您另外指定。
在您的情况下,您可能需要将这四个变量移至BuildPath()
本身。 再说一次,您似乎永远都不会改变它们,所以我将在所有变量中使一个不变。
另外, list
似乎并没有使用,因此可能是多余的。
不是Java的人,我没有方便的编译器来告诉您确切的问题。 尝试制作一个新文件并一次又添加一些代码,看看编译器抱怨什么。
如果可以的话,对我来说,如果您在不真正了解Java语法的情况下进行递归,那么您将超越自己。 首先,我将至少专注于学习基础知识。
从我所看到的(以及我的IDE给此代码带来的错误)中,您遇到了一些基本的编译问题。
在方法main(String [])中 ,引用此行:
buildPath (right, down, path, allPaths, N);
您尚未在main中定义vars“ right”,“ down”,“ allPaths”。 这些是BuildPath(int,int,String,ArrayList,int)中参数的内部名称,您不能从外部使用它们的名称。
BuildPath(int,int,String,ArrayList,int)中发生的事情也是如此,您不能使用“ ol1_right”,“ ol1_down”,因为它们仅在main(String [])中定义。
此行中还有另一个问题:
path += "(" + down "," + right + ")->";
在down之后添加一个+ 。
if语句中的分号还有另一个问题:
else if (right +1 == ol1_right && down == ol1_down) && (right +1 ==
ol2_right && down == ol2_down);{
应为(删除;并添加外部( ) ):
else if ((right +1 == ol1_right && down == ol1_down) && (right +1 ==
ol2_right && down == ol2_down)) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.