最适合网络开发者的网站
Java 编程语言。初学者课程

尿素

Java Math


The Java Math class has many methods that allows you to perform mathematical tasks on numbers.


Math.max(x,y)

The Math.max(x,y) method can be used to find the highest value of xy:

Example

Math.max(5, 10);
Try it Yourself »

Math.min(x,y)

The Math.min(x,y) method can be used to find the lowest value of x and y:

Example

Math.min(5, 10);
Try it Yourself »

Math.sqrt(x)

The Math.sqrt(x) method returns the square root of x:

Example

Math.sqrt(64);
Try it Yourself »

Math.abs(x)

The Math.abs(x) method returns the absolute (positive) value of x:

Example

Math.abs(-4.7);
Try it Yourself »

Random Numbers

Math.random() returns a random number between 0.0 (inclusive), and 1.0 (exclusive):

Example

Math.random();
Try it Yourself »

To get more control over the random number, for example, if you only want a random number between 0 and 100, you can use the following formula:

Example

int randomNum = (int)(Math.random() * 101);  // 0 to 100
Try it Yourself »

Complete Math Reference

For a complete reference of Math methods, go to our Java Math Methods Reference.


Test Yourself With Exercises

Exercise:

Use the correct method to find the highest value of xy.

int x = 5;
int y = 10;
Math.(x, y);