Math类

Math类概述

Math类就是Java自带的数学工具箱,全是静态方法,直接 Math.xxx() 调用,不用new

1
2
3
4
5
6
7
@Test
public void testMathBasic() {
// Math类不能new,也不能被继承(final类)
// 所有方法都是static的,直接用类名调用
System.out.println(Math.PI); // 3.141592653589793
System.out.println(Math.E); // 2.718281828459045
}

常用方法速查表

方法 作用 示例 结果
Math.abs(x) 绝对值 Math.abs(-5) 5
Math.max(a,b) 较大值 Math.max(3, 7) 7
Math.min(a,b) 较小值 Math.min(3, 7) 3
Math.pow(a,b) a的b次方 Math.pow(2, 10) 1024.0
Math.sqrt(x) 平方根 Math.sqrt(16) 4.0
Math.ceil(x) 向上取整 Math.ceil(3.1) 4.0
Math.floor(x) 向下取整 Math.floor(3.9) 3.0
Math.round(x) 四舍五入 Math.round(3.5) 4
Math.random() [0,1)随机数 Math.random() 0.xxxx
Math.log(x) 自然对数(ln) Math.log(Math.E) 1.0
Math.log10(x) 以10为底 Math.log10(100) 2.0

代码示例:常用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
@Test
public void testCommonMethods() {
// === 绝对值 ===
System.out.println(Math.abs(-10)); // 10
System.out.println(Math.abs(10)); // 10

// === 最大最小值 ===
System.out.println(Math.max(3, 7)); // 7
System.out.println(Math.min(3, 7)); // 3
// 求三个数的最大值:嵌套调用
int maxOfThree = Math.max(Math.max(3, 7), 5);
System.out.println("三个数最大值:" + maxOfThree); // 7

// === 幂运算 ===
// 参考 幂与对数 中的详细讲解
System.out.println(Math.pow(2, 10)); // 1024.0(2的10次方)
System.out.println(Math.pow(3, 3)); // 27.0

// === 平方根 ===
System.out.println(Math.sqrt(16)); // 4.0
System.out.println(Math.sqrt(2)); // 1.4142135623730951

// === 取整三兄弟 ===
double num = 3.6;
System.out.println("ceil(3.6) = " + Math.ceil(num)); // 4.0(天花板,往上)
System.out.println("floor(3.6) = " + Math.floor(num)); // 3.0(地板,往下)
System.out.println("round(3.6) = " + Math.round(num)); // 4(四舍五入)

num = -3.6;
System.out.println("ceil(-3.6) = " + Math.ceil(num)); // -3.0(往大的方向)
System.out.println("floor(-3.6) = " + Math.floor(num)); // -4.0(往小的方向)
System.out.println("round(-3.6) = " + Math.round(num)); // -4
}

取整三兄弟的区别

方法 含义 3.2 3.6 -3.2 -3.6
ceil 天花板(往上取) 4.0 4.0 -3.0 -3.0
floor 地板(往下取) 3.0 3.0 -4.0 -4.0
round 四舍五入 3 4 -3 -4

Math.random() 生成指定范围随机数

Math.random() 返回 [0, 1) 之间的double,要生成指定范围需要做点数学

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@Test
public void testRandom() {
// Math.random() 返回 [0, 1) 之间的double
System.out.println(Math.random()); // 0.xxxx

// 公式:生成 [min, max] 范围的随机整数
// (int)(Math.random() * (max - min + 1)) + min

// 生成 [1, 6] 的随机数(模拟骰子)
int dice = (int)(Math.random() * 6) + 1;
System.out.println("骰子:" + dice);

// 生成 [10, 99] 的两位随机数
int twoDigit = (int)(Math.random() * 90) + 10;
System.out.println("两位数:" + twoDigit);

// 生成 [0, 100] 的随机整数
int score = (int)(Math.random() * 101);
System.out.println("分数:" + score);

// 从数组中随机取一个元素
String[] names = {"张三", "李四", "王五", "赵六"};
int index = (int)(Math.random() * names.length);
System.out.println("随机选中:" + names[index]);
}

公式记忆

[0, n) 的整数:(int)(Math.random() * n)

[min, max] 的整数:(int)(Math.random() * (max - min + 1)) + min

关于取余运算在循环索引中的应用,参考 取余与模运算

常见坑

说明
Math.abs(Integer.MIN_VALUE) 还是负数 因为 |Integer.MIN_VALUE| 超出int范围,溢出了
Math.round(-0.5) 是0不是-1 round是”四舍五入”,-0.5入到0
Math.pow 返回double (int)Math.pow(2,3) 需要强转才是int
Math.random() 永远取不到1 范围是 [0, 1),含0不含1

练习题

题1:生成6位随机验证码(数字+字母)

1
2
3
4
5
6
7
8
9
10
@Test
public void testVerificationCode() {
String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
StringBuilder code = new StringBuilder();
for (int i = 0; i < 6; i++) {
int index = (int)(Math.random() * chars.length());
code.append(chars.charAt(index));
}
System.out.println("验证码:" + code);
}

题2:模拟猜数字游戏

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Test
public void testGuessNumber() {
int target = (int)(Math.random() * 100) + 1; // [1, 100]
System.out.println("(偷看答案:" + target + ")");

// 模拟几次猜测
int guess = 50;
if (guess > target) {
System.out.println("猜大了");
} else if (guess < target) {
System.out.println("猜小了");
} else {
System.out.println("猜对了!");
}
// 实际应用中用Scanner读取用户输入 + while循环
}

题3:计算两点之间的距离

1
2
3
4
5
6
7
8
@Test
public void testDistance() {
// 两点距离公式:sqrt((x1-x2)² + (y1-y2)²)
double x1 = 1, y1 = 2;
double x2 = 4, y2 = 6;
double distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
System.out.println("两点距离:" + distance); // 5.0
}

上一章 目录 下一章
Object类 java基础 Collection体系