Java 调用递归方法的不可访问语句
在编写这段代码时,每当我试图在递归方法中达到[x]时,我在尝试编译时都会遇到“unreachable statement”错误Java 调用递归方法的不可访问语句,java,recursion,Java,Recursion,在编写这段代码时,每当我试图在递归方法中达到[x]时,我在尝试编译时都会遇到“unreachable statement”错误 public class recursion { public static boolean match (int [] a, int [] pattern) { if(pattern.length==0) return true; boolean x; x=match(a,pattern,0,0); if(x==true
public class recursion
{
public static boolean match (int [] a, int [] pattern)
{
if(pattern.length==0)
return true;
boolean x;
x=match(a,pattern,0,0);
if(x==true)
return true;
return false;
}
public static boolean match (int [] a, int [] pattern,int aCounter,int ptCounter)
{
int count=0;
int x=aCounter;
if(x==a.length);
{
if(count==pattern.length)
return true;
else return false;
}
if(a[x]>100)
{
count=0;
return match(a,pattern,aCounter+1,0);
}
else if(((pattern[ptCounter]==1)||(pattern[ptCounter]==0))&&((a[x]>-10)&&(a[x]<10)))
{
count++;
return match(a,pattern,aCounter+1,ptCounter+1);
}
else if(((pattern[ptCounter]==2)||(pattern[ptCounter]==0))&&(((a[x]<-10)&&(a[x]>-100))||((a[x]>9)&&(a[x]<100))))
{
count++;
return match(a,pattern,aCounter+1,ptCounter+1);
}
}
}
公共类递归
{
公共静态布尔匹配(int[]a,int[]pattern)
{
if(pattern.length==0)
返回true;
布尔x;
x=匹配(a,模式,0,0);
如果(x==true)
返回true;
返回false;
}
公共静态布尔匹配(int[]a,int[]pattern,int-aCounter,int-ptCounter)
{
整数计数=0;
int x=计数器;
如果(x==a.长度);
{
if(count==pattern.length)
返回true;
否则返回false;
}
如果(a[x]>100)
{
计数=0;
返回匹配(a,模式,计数器+1,0);
}
否则如果((模式[ptCounter]==1)| |(模式[ptCounter]==0))&&&((a[x]>-10)&&&(a[x]9)&&&(a[x]您的问题是不必要的;
:
if(x==a.length); // here
{
if(count==pattern.length)
return true;
else return false;
}
此;
关闭if语句,因此始终执行以下块(并返回true
或false
),并且该块之后的代码将无法访问。您的问题是不必要的;
:
if(x==a.length); // here
{
if(count==pattern.length)
return true;
else return false;
}
此;
关闭if语句,因此始终执行以下bock(并返回true
或false
),并且该块后的代码变得不可访问。不是答案,而是观察:而不是if(条件)返回true;否则返回false;
你应该只做返回条件
。不是回答,只是观察:如果(条件)返回true,而不是;否则返回false;
你应该只做返回条件
。