public static void main(String []args)
{
int x =5;
System.out.println(x + " to the power 4 is " +power(x,4));
System.out.println("7 to the power 5 is " +power(7,5));
System.out.println(x + " to the power 0 is " +power(7,0));
System.out.println("10 to the power -2 is " +power(10,-2));
}
static int power(double x,int n)
{
if(n>1)
return (int) (x*power(x,n-1));
else if(n<0)
return 1/power(x,-n);
else
return (int) (n==0?1:x);
}
这个程序输出如下:
5 to the power 4 is 625 //625这个是怎么得出来的?请告诉我计算方法?这个递归我看不出来为什么会得出如此结果
7.5 to the power 5 is 16807
5 to the power 0 is 1
10 to the power -2 is 0
{
int x =5;
System.out.println(x + " to the power 4 is " +power(x,4));
System.out.println("7 to the power 5 is " +power(7,5));
System.out.println(x + " to the power 0 is " +power(7,0));
System.out.println("10 to the power -2 is " +power(10,-2));
}
static int power(double x,int n)
{
if(n>1)
return (int) (x*power(x,n-1));
else if(n<0)
return 1/power(x,-n);
else
return (int) (n==0?1:x);
}
这个程序输出如下:
5 to the power 4 is 625 //625这个是怎么得出来的?请告诉我计算方法?这个递归我看不出来为什么会得出如此结果
7.5 to the power 5 is 16807
5 to the power 0 is 1
10 to the power -2 is 0
解决方案 »
- 求一将字符串等分割的正则表达式
- 大家可以到这里下载火狐浏览器,很安全,快速的上网
- 如何创建一个文件可以覆盖原来的文件,如果它存在?
- 如何让类只能被实例化一个对象,也就是只能实例一次呢?
- 在线等待!急(数据库的连接问题)
- 定义两个类A类和B类,怎么令A类只能经过B类的方法来实例化,而不能直接用new?
- 请高手回答,不够可以再加分
- 帮我看看java为什么不能运行(源代码)
- 请教高手学习java什么比较好!!!!!!!!!!!!!
- 在windows me 下,jdk1.2 如何配置jdbc:odbc桥连接access
- 有关多线程和异常处理 我彻底凌乱了 一个初学者的自白求助帖
- 运行服务器端,抛出异常Exception in thread "Thread-0" java.lang.NullPointerException at com
7 to the power 5 =7*7*7*7*7*1=16807
5 to the power 0 = 1
10 to the power -2 =1/(10*10*1)=0 是整除