Java是否具有指数运算符?

Java中是否有指数运算符?

例如,如果提示用户输入两个数字,然后他们输入32,则正确答案为9

import java.util.Scanner;

public class Exponentiation {

public static double powerOf (double p) {

double pCubed;

pCubed = p*p;

return (pCubed);

}

public static void main (String [] args) {

Scanner in = new Scanner (System.in);

double num = 2.0;

double cube;

System.out.print ("Please put two numbers: ");

num = in.nextInt();

cube = powerOf(num);

System.out.println (cube);

}

}

回答:

要通过用户输入执行此操作:

public static void getPow(){

Scanner sc = new Scanner(System.in);

System.out.println("Enter first integer: "); // 3

int first = sc.nextInt();

System.out.println("Enter second integer: "); // 2

int second = sc.nextInt();

System.out.println(first + " to the power of " + second + " is " +

(int) Math.pow(first, second)); // outputs 9

以上是 Java是否具有指数运算符? 的全部内容, 来源链接: utcz.com/qa/425233.html

回到顶部