java - 在java中使用命令行参数时没有得到正确的输出
问题描述
//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);
}
}
推荐阅读
- c++ - VS Code 使用 Ctrl - Alt - M 终止任务将不起作用
- identity - 尝试在 Blazor WASM 应用程序中搭建身份页面时出错
- ansible - Ansible Jinja2 Filter - 过滤数据并从数据创建列表
- java - 尝试运行 spring mvc-hibernate 应用程序时出现异常
- python - Django:如何在对称的多对多关系中获取引用对象?
- python - 将颜色过滤器添加到 Tk.Label
- c++ - 如何使用 MinGw G++ 编译器增加堆栈大小?
- laravel - 是否可以将 2 个变量传递给 View Composers 中的视图?
- java - MongoDB 4.4、Java 驱动程序 4.2.3 - InsertManyResult.getInsertedIds() 不返回所有插入文档的 ID
- sumo - 如何在当前的 SUMO 场景中添加额外的路线?