编写递归程序Java

2025-01-07 20:49:42
推荐回答(2个)
回答1:

public class bb
{
public static void main(String args[])
{

System.out.println(getpower(2,10));
}
public static int getpower(int x,int y)//此为您要的递归方法

{
if(y==0)
return 1;
else if(y==1)
return x;
else return x*getpower(x,y-1);
}
}

回答2:

public static void main(String args[])
{

System.out.println(getpower(2,10));
}
public static int getpower(int x,int y){
if(y==1)
return x;
else
return x*getpower(x,--y);
}