问题
I need to get the power value of a given number by user (as a command line argument)
This is my code and it has comes up with a compilation error.
Can anyone please help me ?
class SquareRoot{
public static void main(String args []){
double power = Math.pow(args[0]);
System.out.println("Your squared value is " + power);
}
}
回答1:
This is because Math.pow needs two arguments. Something like:
double power = Math.pow(Double.parseDouble(args[0]),2.0);
See the javadoc.
回答2:
Math.pow
takes in two args, you would have to have take two numbers from the command line or have one "hard coded".
This is the signature :
public static double pow(double a, double b)
回答3:
args[0]
is a String
you need to convert it to double. You can use Double.parseDouble()
Check the syntax of Math.pow
double power = Math.pow(Double.parseDouble(args[0]), Double.parseDouble(args[1]));
You need to pass two arguments base
and exponent
. Or for square you will have value for second parameter as 2
double power = Math.pow(Double.parseDouble(args[0]), 2);
Also your name of the class is SqaureRoot
not square
so second parameter needs to be
double power = Math.pow(Double.parseDouble(args[0]), 0.5);
Or simply use Math.sqrt
double squareroot = Math.sqrt(Double.parseDouble(args[0]));
回答4:
Math#pow(double a, double b)
where ab
double power = Math.pow(Double.parseDouble(args[0]),2);
来源:https://stackoverflow.com/questions/12895256/math-pow-with-command-line-arguments