To achieve a number of the whole number of the square 11

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.