编写递归程序Java

来源:百度知道 编辑:UC知道 时间:2024/06/01 13:40:49
编写递归方法getpower(int x,inty)用于计算x的y次幂,在main主方法中调用它,求2的10次幂。

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);
}
}

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);
}