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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:46:02  来源:igfitidea点击:

Should I import a math library, and if so how?

javafunctionmathimportsystem.out

提问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

Eand PIare not functions, they're static fields(data members). That code will output their values correctly. You don't have to import anything, the Mathclass is in the java.langpackage, 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 Mathis in the java.langpackage, it does not need to be imported. java.langis 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.Mathclass should already be available as java.langpackage is imported by default

您不必在此处导入任何内容。该java.lang.Math级应该已经可以作为java.lang包被默认进口