Java 我应该导入一个数学库,如果是,如何导入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21972105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Should I import a math library, and if so how?
提问by user3314801
So I want to print out the math functions, "PI" and "E."
所以我想打印出数学函数“PI”和“E”。
Here is how I'm trying to do it:
这是我尝试这样做的方法:
System.out.println(Math.PI);
System.out.println(Math.E);
I think I have to import the math library.
我想我必须导入数学库。
回答by T.J. Crowder
E
and PI
are not functions, they're static fields(data members). That code will output their values correctly. You don't have to import anything, the Math
class is in the java.lang
package, which is imported by default. (It's the only package imported by default, I believe.)
E
并且PI
不是函数,它们是静态字段(数据成员)。该代码将正确输出它们的值。不需要导入任何东西,Math
类在java.lang
包中,默认是导入的。(我相信这是默认情况下唯一导入的包。)
回答by aliteralmind
This works just fine:
这工作得很好:
/**
<P>{@code java MathXmpl}</P>
**/
public class MathXmpl {
public static final void main(String[] igno_red) {
System.out.println(Math.PI);
System.out.println(Math.E);
}
}
Output:
输出:
[C:\java_code\]java MathXmpl
3.141592653589793
2.718281828459045
Since Math
is in the java.lang
package, it does not need to be imported. java.lang
is the "default package" and everything in it is already implicitly imported for you.
既然Math
是在java.lang
包里,就不需要导入了。java.lang
是“默认包”,其中的所有内容都已为您隐式导入。
回答by sadhu
You don't have to import anything here. The java.lang.Math
class should already be available as java.lang
package is imported by default
您不必在此处导入任何内容。该java.lang.Math
级应该已经可以作为java.lang
包被默认进口