文章目录
-
- [1. 取最大值:`Math.max()`](#1. 取最大值:
Math.max()) - [2. 取最小值:`Math.min()`](#2. 取最小值:
Math.min()) - [3. 绝对值:`Math.abs()`](#3. 绝对值:
Math.abs()) - [4. 平方根:`Math.sqrt()`](#4. 平方根:
Math.sqrt()) - [5. 幂运算:`Math.pow()`](#5. 幂运算:
Math.pow()) - [6. 向上取整:`Math.ceil()`](#6. 向上取整:
Math.ceil()) - [7. 向下取整:`Math.floor()`](#7. 向下取整:
Math.floor()) - [8. 四舍五入:`Math.round()`](#8. 四舍五入:
Math.round()) - [9. 随机数:`Math.random()`](#9. 随机数:
Math.random()) - [10. 常用常量](#10. 常用常量)
- [12. 机试中最常用的方法](#12. 机试中最常用的方法)
- 总结
- [1. 取最大值:`Math.max()`](#1. 取最大值:
| 方法 | 作用 | 示例 | 结果 |
|---|---|---|---|
Math.max(a, b) |
取最大值 | Math.max(3, 5) |
5 |
Math.min(a, b) |
取最小值 | Math.min(3, 5) |
3 |
Math.abs(x) |
取绝对值 | Math.abs(-8) |
8 |
Math.sqrt(x) |
开平方 | Math.sqrt(16) |
4.0 |
Math.pow(a, b) |
幂运算 | Math.pow(2, 3) |
8.0 |
Math.ceil(x) |
向上取整 | Math.ceil(3.2) |
4.0 |
Math.floor(x) |
向下取整 | Math.floor(3.8) |
3.0 |
Math.round(x) |
四舍五入 | Math.round(3.5) |
4 |
Math.random() |
随机数 | Math.random() |
0.0 ~ 1.0 |
Math 是 Java 提供的数学工具类,常用于数值计算、取最大最小值、绝对值、平方根、幂运算、取整、随机数等场景。
使用时不需要创建对象,直接通过 Math.方法名() 调用即可。
1. 取最大值:Math.max()
用于返回两个数中的较大值。
java
int a = 10;
int b = 20;
System.out.println(Math.max(a, b)); // 20
常见用法:更新最大值。
java
max = Math.max(max, num);
2. 取最小值:Math.min()
用于返回两个数中的较小值。
java
int a = 10;
int b = 20;
System.out.println(Math.min(a, b)); // 10
常见用法:更新最小值。
java
min = Math.min(min, num);
3. 绝对值:Math.abs()
用于返回一个数的绝对值。
java
System.out.println(Math.abs(-5)); // 5
System.out.println(Math.abs(5)); // 5
常见用法:计算两个数的差值。
java
int diff = Math.abs(a - b);
4. 平方根:Math.sqrt()
用于计算一个数的平方根,返回值是 double 类型。
java
System.out.println(Math.sqrt(9)); // 3.0
System.out.println(Math.sqrt(16)); // 4.0
如果需要整数,可以强制转换:
java
int root = (int) Math.sqrt(16);
System.out.println(root); // 4
5. 幂运算:Math.pow()
用于计算 a 的 b 次方,返回值是 double 类型。
java
System.out.println(Math.pow(2, 3)); // 8.0
System.out.println(Math.pow(10, 2)); // 100.0
如果需要整数:
java
int x = (int) Math.pow(2, 3);
System.out.println(x); // 8
6. 向上取整:Math.ceil()
用于向上取整,返回值是 double 类型。
java
System.out.println(Math.ceil(3.1)); // 4.0
System.out.println(Math.ceil(3.9)); // 4.0
特点:只要有小数部分,就往上取整。
7. 向下取整:Math.floor()
用于向下取整,返回值是 double 类型。
java
System.out.println(Math.floor(3.1)); // 3.0
System.out.println(Math.floor(3.9)); // 3.0
特点:直接去掉小数部分,向下取整。
8. 四舍五入:Math.round()
用于四舍五入。
java
System.out.println(Math.round(3.4)); // 3
System.out.println(Math.round(3.5)); // 4
System.out.println(Math.round(3.9)); // 4
9. 随机数:Math.random()
Math.random() 会返回一个 [0.0, 1.0) 范围内的随机小数。
java
System.out.println(Math.random());
生成 1 ~ 10 的随机整数:
java
int num = (int)(Math.random() * 10) + 1;
System.out.println(num);
解释:
java
Math.random() * 10
生成范围是:
[0.0, 10.0)
再强制转成 int,范围就是:
0 ~ 9
最后加 1,范围就是:
1 ~ 10
10. 常用常量
圆周率:Math.PI
java
System.out.println(Math.PI);
输出:
3.141592653589793
自然常数:Math.E
java
System.out.println(Math.E);
输出:
2.718281828459045
12. 机试中最常用的方法
在算法题和机试中,最常用的是下面几个:
java
Math.max()
Math.min()
Math.abs()
Math.sqrt()
Math.pow()
其中 Math.max() 和 Math.min() 经常用于更新最大值、最小值。
例如:
java
int ans = 0;
for (int num : arr) {
ans = Math.max(ans, num);
}
总结
Math 是 Java 中非常常用的数学工具类,常见方法可以按用途记忆:
最大最小:max / min
绝对值:abs
平方根:sqrt
幂运算:pow
取整:ceil / floor / round
随机数:random
常量:PI / E
日常刷题时,优先掌握 max、min、abs、sqrt、pow 这几个方法即可。