The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
The Java programming language supports basic arithmetic with its arithmetic operators: +, -, *, /, and %.Java编程语言支持基本算术及其算术运算符:+
、-
、*
、/
和%
。The Math
class in the java.lang
package provides methods and constants for doing more advanced mathematical computation.java.lang
包中的Math
类提供了进行更高级数学计算的方法和常量。
The methods in the Math
class are all static, so you call them directly from the class, like this:Math
类中的方法都是静态的,因此可以直接从类中调用它们,如下所示:
Math.cos(angle);
static import
language feature, you don't have to write Math
in front of every math function:static import
语言功能,您不必在每个数学函数前面写Math
:
import static java.lang.Math.*;
This allows you to invoke the 这允许您通过简单的名称调用Math
class methods by their simple names.Math
类方法。For example:例如:
cos(angle);
The Math
class includes two constants:Math
类包括两个常量:
Math.E
Math.PI
The Math
class also includes more than 40 static methods.Math
类还包括40多个静态方法。The following table lists a number of the basic methods.下表列出了一些基本方法。
double abs(double d) |
|
double ceil(double d) |
|
double floor(double d) |
|
double rint(double d) |
|
long round(double d) |
long 或int ,如方法的返回类型所示。 |
double min(double arg1, double arg2) |
|
double max(double arg1, double arg2) |
The following program, 以下程序BasicMathDemo
, illustrates how to use some of these methods:BasicMathDemo
演示了如何使用其中一些方法:
public class BasicMathDemo { public static void main(String[] args) { double a = -191.635; double b = 43.74; int c = 16, d = 45; System.out.printf("The absolute value " + "of %.3f is %.3f%n", a, Math.abs(a)); System.out.printf("The ceiling of " + "%.2f is %.0f%n", b, Math.ceil(b)); System.out.printf("The floor of " + "%.2f is %.0f%n", b, Math.floor(b)); System.out.printf("The rint of %.2f " + "is %.0f%n", b, Math.rint(b)); System.out.printf("The max of %d and " + "%d is %d%n", c, d, Math.max(c, d)); System.out.printf("The min of of %d " + "and %d is %d%n", c, d, Math.min(c, d)); } }
Here's the output from this program:以下是该程序的输出:
The absolute value of -191.635 is 191.635 The ceiling of 43.74 is 44 The floor of 43.74 is 43 The rint of 43.74 is 44 The max of 16 and 45 is 45 The min of 16 and 45 is 16
The next table lists exponential and logarithmic methods of the 下表列出了Math
class.Math
类的指数和对数方法。
double exp(double d) |
|
double log(double d) |
|
double pow(double base, double exponent) |
|
double sqrt(double d) |
The following program, 下面的程序ExponentialDemo
, displays the value of e
, then calls each of the methods listed in the previous table on arbitrarily chosen numbers:ExponentialDemo
显示e
的值,然后对任意选择的数字调用上表中列出的每个方法:
public class ExponentialDemo { public static void main(String[] args) { double x = 11.635; double y = 2.76; System.out.printf("The value of " + "e is %.4f%n", Math.E); System.out.printf("exp(%.3f) " + "is %.3f%n", x, Math.exp(x)); System.out.printf("log(%.3f) is " + "%.3f%n", x, Math.log(x)); System.out.printf("pow(%.3f, %.3f) " + "is %.3f%n", x, y, Math.pow(x, y)); System.out.printf("sqrt(%.3f) is " + "%.3f%n", x, Math.sqrt(x)); } }
Here's the output you'll see when you run 以下是运行ExponentialDemo
:ExponentialDemo
时将看到的输出:
The value of e is 2.7183 exp(11.635) is 112983.831 log(11.635) is 2.454 pow(11.635, 2.760) is 874.008 sqrt(11.635) is 3.411
The Math
class also provides a collection of trigonometric functions, which are summarized in the following table.Math
类还提供了三角函数的集合,下表总结了这些函数。The value passed into each of these methods is an angle expressed in radians.传递到每个方法中的值都是以弧度表示的角度。You can use the 可以使用toRadians
method to convert from degrees to radians.toRadians
方法将度转换为弧度。
double sin(double d) |
|
double cos(double d) |
|
double tan(double d) |
|
double asin(double d) |
|
double acos(double d) |
|
double atan(double d) |
|
double atan2(double y, double x) |
(x, y) to polar coordinate (r, theta) and returns theta .(x, y) 转换为极坐标(r, theta) 并返回theta 。 |
double toDegrees(double d) |
Here's a program, 这是一个名为TrigonometricDemo
, that uses each of these methods to compute various trigonometric values for a 45-degree angle:TrigonometricDemo
的程序,它使用这些方法计算45度角的各种三角值:
public class TrigonometricDemo { public static void main(String[] args) { double degrees = 45.0; double radians = Math.toRadians(degrees); System.out.format("The value of pi " + "is %.4f%n", Math.PI); System.out.format("The sine of %.1f " + "degrees is %.4f%n", degrees, Math.sin(radians)); System.out.format("The cosine of %.1f " + "degrees is %.4f%n", degrees, Math.cos(radians)); System.out.format("The tangent of %.1f " + "degrees is %.4f%n", degrees, Math.tan(radians)); System.out.format("The arcsine of %.4f " + "is %.4f degrees %n", Math.sin(radians), Math.toDegrees(Math.asin(Math.sin(radians)))); System.out.format("The arccosine of %.4f " + "is %.4f degrees %n", Math.cos(radians), Math.toDegrees(Math.acos(Math.cos(radians)))); System.out.format("The arctangent of %.4f " + "is %.4f degrees %n", Math.tan(radians), Math.toDegrees(Math.atan(Math.tan(radians)))); } }
The output of this program is as follows:该程序的输出如下:
The value of pi is 3.1416 The sine of 45.0 degrees is 0.7071 The cosine of 45.0 degrees is 0.7071 The tangent of 45.0 degrees is 1.0000 The arcsine of 0.7071 is 45.0000 degrees The arccosine of 0.7071 is 45.0000 degrees The arctangent of 1.0000 is 45.0000 degrees
The random()
method returns a pseudo-randomly selected number between 0.0 and 1.0.random()
方法返回一个介于0.0和1.0之间的伪随机数。The range includes 0.0 but not 1.0.范围包括0.0,但不包括1.0。In other words: 换句话说:0.0 <= Math.random() < 1.0
.0.0<=Math.random()<1.0
。To get a number in a different range, you can perform arithmetic on the value returned by the random method.要获得不同范围内的数字,可以对随机方法返回的值执行算术运算。For example, to generate an integer between 0 and 9, you would write:例如,要生成介于0和9之间的整数,可以编写:
int number = (int)(Math.random() * 10);
By multiplying the value by 10, the range of possible values becomes 通过将该值乘以0.0 <= number < 10.0
.10
,可能值的范围变为0.0 <= number < 10.0
。
Using 当需要生成单个随机数时,使用Math.random
works well when you need to generate a single random number.Math.random
效果很好。If you need to generate a series of random numbers, you should create an instance of 如果需要生成一系列随机数,则应创建java.util.Random
and invoke methods on that object to generate numbers.java.util.Random
的实例,并调用该对象上的方法来生成数字。