Math类概述
Math类就是Java自带的数学工具箱,全是静态方法,直接 Math.xxx() 调用,不用new
1 2 3 4 5 6 7
| @Test public void testMathBasic() {
System.out.println(Math.PI); System.out.println(Math.E); }
|
常用方法速查表
| 方法 |
作用 |
示例 |
结果 |
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)); System.out.println(Math.abs(10));
System.out.println(Math.max(3, 7)); System.out.println(Math.min(3, 7));
int maxOfThree = Math.max(Math.max(3, 7), 5); System.out.println("三个数最大值:" + maxOfThree);
System.out.println(Math.pow(2, 10)); System.out.println(Math.pow(3, 3));
System.out.println(Math.sqrt(16)); System.out.println(Math.sqrt(2));
double num = 3.6; System.out.println("ceil(3.6) = " + Math.ceil(num)); System.out.println("floor(3.6) = " + Math.floor(num)); System.out.println("round(3.6) = " + Math.round(num));
num = -3.6; System.out.println("ceil(-3.6) = " + Math.ceil(num)); System.out.println("floor(-3.6) = " + Math.floor(num)); System.out.println("round(-3.6) = " + Math.round(num)); }
|
取整三兄弟的区别
| 方法 |
含义 |
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() {
System.out.println(Math.random());
int dice = (int)(Math.random() * 6) + 1; System.out.println("骰子:" + dice);
int twoDigit = (int)(Math.random() * 90) + 10; System.out.println("两位数:" + twoDigit);
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; System.out.println("(偷看答案:" + target + ")");
int guess = 50; if (guess > target) { System.out.println("猜大了"); } else if (guess < target) { System.out.println("猜小了"); } else { System.out.println("猜对了!"); }
}
|
题3:计算两点之间的距离
1 2 3 4 5 6 7 8
| @Test public void testDistance() {
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); }
|