java 求幂 急!

Python016

java 求幂 急!,第1张

import java.math.BigDecimal

import java.util.Scanner

public class Cat {

public static void main(String[] args) {

System.out.print("Please input 2 numbers:")

String[] input = new Scanner(System.in).nextLine().trim().split("\\s+")

BigDecimal num1 = new BigDecimal(input[0])

int num2 = Integer.parseInt(input[1])

BigDecimal mul = new BigDecimal("1.0")

for(int i = 1i <= num2i++){

mul = mul.multiply(num1)

}

System.out.println(mul.toString())

}

}

------------------testing

Please input 2 numbers:95.123 12

548815620517731830194541.8990253434157159735359672218698527210

import java.util.Scanner

public class test005 {

double myPow(double x, int y) {

double pow = 0

if (y >0) {

pow = x * myPow(x, y - 1)// 2,3//2*2,3-1

}

if (y <0) {

pow = 1 / x * myPow(x, y + 1)

}

if (y == 0) {

pow = 1

}

return pow

}

void count() {

System.out.println("please input base number and index number")

double base = new Scanner(System.in).nextInt()

int index = new Scanner(System.in).nextInt()

double result = myPow(base, index)

System.out.println(result)

}

public static void main(String[] s) {

new test005().count()

}

}

方法一:

public class TestMath{

public static void main(String[] args){

System.out.println("2^3="+Math.pow(2,3)) //直接调用Java中求幂的方法pow(),输出的结果为2的3次幂。

}

}

方法二:

public calss Test{

public static void main(String[] args){

//示例:求3的5次幂

int a=3

int b=5

for(int i=1i<bi++)

a*=a

System.out.println("3的5次幂等于"+a)

}

}