【发布时间】:2020-02-01 05:07:44
【问题描述】:
//Square root of a no. using command line argument
class Calculator {
double i;
double x = Math.sqrt(i);
}
class SquareRoot {
public static void main(String arg[]) {
Calculator a = new Calculator();
a.i = Integer.parseInt(arg[0]);
System.out.println("The square root of " + a.i + " is " + a.x);
}
}
我的输出:
The square root of 64 is 0.0
我的代码有什么问题?
【问题讨论】:
-
因为
double x=Math.sqrt(i);是在a.i=Integer.parseInt(arg[0]);之前计算出来的(而且是0)。 -
所以要纠正它,我需要先创建一个方法,然后调用它。对吗?