Note the input judgment, write more than a few if statements to determine whether the condition of the input data is satisfied
??
In addition, when exp is negative, the input base becomes 1.0/base instead of 1/base
??
So the return value is set to double,base is also set to double
??
In addition, you have to define the equal, and can not use = =, to determine whether the absolute value of the two-digit difference is within a certain range
??
Package myPower11;
??
public class MyPower11 {
Static Boolean equal (double num1,double num2)
{
if ((num1-num2>-0.0000001) &&num1-num2<0.0000001)
{
return true;
}
Else
{
return false;
}
}
Static double Mypower (double base, int exp) throws Exception {
Double result = 0;
if (equal (base,0.0) && Exp < 0) {
throw new Exception ("no sense");
}
if (equal (base,0.0) && exp >= 0) {
return 0;
}
if (base!=0&&exp>=0) {
Result=powercal (base, exp);
return result;
}
if (base! = 0 && Exp < 0) {
Return powercal (1.0/base,-exp);
}
return result;
??
}
??
Static double powercal (double base, int exp) {
double result = 1;
if (base! = 0 && Exp >= 0) {
for (int i = 0; i < exp; i++) {
Result *= base;
}
??
}
return result;
??
}
??
public static void Main (string[] args) throws Exception {
TODO auto-generated Method Stub
System.out.println (Mypower (2,-3));
}
??
}
? ?
To achieve a number of the whole number of the square 11