Math.pow with Command line arguments

拥有回忆 提交于 2021-01-28 05:58:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!