在Java中使用命令行参数时无法获得正确的输出

Aps:
//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

我的代码有什么问题?

普拉特帕特尔:

试试这个 :

class Calculator {
    double i, x;
    void squareRoot() {
        x = Math.sqrt(i);
    }
}

class SquareRoot {
    public static void main(String arg[]) {
        Calculator a = new Calculator();
        a.i = Integer.parseInt(arg[0]);
        a.squareRoot();
        System.out.println("The square root of " + a.i + " is " + a.x);
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用命令行参数时无法写入文件

如何在命令行中使用命令输出时转义空格

在python中使用命令行参数将变量设置为c程序的输出

在JavaFX和JavaFX中使用命令行参数

如何在Clojure中使用命令行参数?

如何在JMeter中使用命令行参数

在J中使用命令行参数

使用命令行参数时出现分段错误

使用命令行参数加载属性文件-Java

如何在Java程序中使用命令行?

如何在C中使用命令行参数并评估为int或double时检查边缘情况?

如何使用命令行参数读取多个文件并使用pandas保存单个输出

尝试使用命令行参数时,将ls命令传递给for循环中断

正确使用Java -D命令行参数

使用命令行参数覆盖Nextflow参数

Java命令行参数交叉输出

如何在Mac OS中使用命令行参数运行应用程序

如何在Angularjs量角器中使用命令行参数?

在除entrypoint.sh以外的文件中使用命令行参数的值

如何在python数据帧中使用命令行参数?

在 perl 中使用命令行参数作为文件名

在Visual Studio中使用命令行参数进行调试

如何在任务中使用命令行参数退出应用程序

在C#单元测试中使用命令行参数

修改R脚本以在Snakemake中使用命令行参数

如何在c(OS:Windows)中使用命令行参数?

在Scala脚本中使用命令行参数的惯用方式

在 bash 脚本中使用命令行参数作为 cp 和 mv 的目标

使用“ gcloud run deploy”时,如何使用命令行参数指定服务名称?