public class Solution {
public double Power(double base, int exponent) {
double result = 1.0;
if (exponent == 0)
return 1;
else if (exponent > 0)
{
for (int i = 1; i <= exponent; i++)
{
result *= base;
}
} else if (base == 0)
throw new RuntimeException("分母不能为零");
for (int i = 1; i <= -exponent; i++)
{
result *= base;
}
return exponent > 0 ? result : (1 / result);
}
public static void main(String[] args)
{
double base = 0;
int exponent = 2;
Solution s = new Solution();
System.out.println(s.Power(base, exponent));
}
}
java实现给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
最新推荐文章于 2021-04-28 21:50:55 发布