有些方法不需要对象,直接通过类名调用——这就是静态方法。本讲以 Math 为例,掌握随机数、绝对值、最值、幂与根号。
用故事建立直觉,用类比理解抽象概念。
修车师傅不会每次现做一把扳手,而是开的工具箱里早有现成的。Java 的 Math 类就是这么一个「数学工具箱」:开方、取整、取最大最小、随机数……都给你备好了。你只需伸手拿,不必自己造轮子。
➡️ 本讲掌握 Math 类的常用静态方法(无需 new,直接用 Math.xxx() 调用)以及「静态方法」的概念。
编程定义:静态方法(static)属于类本身,可直接用 类名.方法() 调用,无需创建对象。Math 的方法都是静态的。
编程定义:Math 提供 abs/round/floor/ceil/sqrt/pow/max/min/random 等常用数学方法,全部静态。
编程定义:
有些功能不需要「某个具体对象」来做——比如求绝对值、取整、随机数,它们是「工具函数」,谁都能直接用。Java 把它们收进 Math 这个工具类,用 Math.方法() 直接调用,不用 new。这种「属于类本身」的方法叫静态方法。
➡️ 本页掌握 Math 的常用静态方法,以及 static 的含义。
语法格式拆解标注,易混概念对比辨析,案例由浅入深。
1double r = Math.sqrt(9); // 3.0
2int a = Math.abs(-5); // 5
3int x = (int)Math.round(3.6); // 4
4int m = Math.max(3, 7); // 7
5double d = Math.random(); // [0,1)Math.sqrt — 平方根Math.abs — 绝对值Math.round — 四舍五入成长整Math.random — 返回 [0,1) 随机小数| 对比项 | A | B |
|---|---|---|
| round | 四舍五入 | 3.6→4 |
| floor | 向下取整 | 3.9→3 |
| ceil | 向上取整 | 3.1→4 |
1System.out.println(Math.sqrt(16)); // 4.0
2System.out.println(Math.abs(-8)); // 84.0
81System.out.println(Math.round(2.5)); // 3
2System.out.println(Math.max(4, 9)); // 9
3System.out.println(Math.min(4, 9)); // 43
9
41System.out.println(Math.floor(3.9)); // 3.0
2System.out.println(Math.ceil(3.1)); // 4.03.0
4.01System.out.println(Math.pow(2, 10)); // 1024.0
2System.out.println(Math.random()); // 如 0.371024.0
0.37...1int n = (int)(Math.random() * 6) + 1; // [1,6] 骰子
2System.out.println(n);(1~6 之一)1int a = (int)Math.random()*10; // 0~9
2int b = Math.abs(-5); // 5
3int c = Math.max(3,8); // 8a∈0~9, b=5, c=81Math.round(3.6); // 4(四舍五入)
2Math.floor(3.6); // 3(向下)
3Math.ceil(3.6); // 4(向上)4,3,41int r=(int)(Math.random()*N); // 0~N-1
2int r=(int)(Math.random()*N)+1; // 1~N先缩放后偏移编程定义:
编程定义:
1int dice = (int)(Math.random() * 6) + 1;
2System.out.println(dice);1~6 随机完整题目 + 输入输出样例,多解法对比,代码注释与运行结果。
题目:用 Math.random() 模拟掷两个六面骰子(1~6),各掷一次,输出两次点数与「是否相等」。
(无输入,随机)骰子A=3 骰子B=5 不相同题目分析:各生成一个 [1,6] 随机整数,用 == 比较是否相等并输出结论。
1public class Dice {
2 public static void main(String[] a) {
3 int x = (int)(Math.random() * 6) + 1;
4 int y = (int)(Math.random() * 6) + 1;
5 System.out.println("骰子A=" + x + " 骰子B=" + y);
6 System.out.println(x == y ? "相同" : "不相同");
7 }
8}骰子A=3 骰子B=5
不相同说明:随机区间公式 (int)(random()*6)+1。
题目分析:抽出 roll() 方法返回骰子点数,main 调用两次,结构更清晰、可复用。
1static int roll() { return (int)(Math.random() * 6) + 1; }
2int a = roll(), b = roll();
3System.out.println("骰子A=" + a + " 骰子B=" + b);骰子A=2 骰子B=2
相同说明:方法提升可读性。
题目:用 Math.random() 写出产生 1 到 6 整数(含两端)的表达式,并解释偏移与缩放。
无1~6 的整数题目分析:(int)(Math.random()*6) 得 0~5,再 +1 偏移到 1~6。
1int d = (int)(Math.random()*6) + 1;1~6说明:先缩放后偏移。
题目分析:先算 0~5 存变量,再加 1,便于调试理解。
1int r=(int)(Math.random()*6); int d=r+1;1~6说明:分步更清楚。
题目:实现两个工具:(1) 把 2.6 四舍五入;(2) 生成 [10,20] 的随机整数。说明 random 的范围变换公式。
值 2.6;范围[10,20]3;10~20题目分析:round 直接四舍五入;范围 [a,b] 整数 = a + (int)(Math.random()*(b-a+1))。
1int r1 = Math.round(2.6f); // 3
2int lo=10, hi=20;
3int r2 = lo + (int)(Math.random() * (hi - lo + 1));3;10~20说明:上限要 +1 才包含 hi。
归纳核心要点与代码万能模板,拓展思考适用场景。
1类名.静态方法();
2int n = (int)(Math.random()*6)+1; // [1,6]1Math.round(2.5) // 33说明:四舍五入。
1Math.floor(2.9) // 22说明:向下/上取整。
1(int)(Math.random()*6)+11~6说明:骰子。
1(int)(Math.random()*6)0~5说明:从0起。
1Math.max(a,b)较大者说明:内置。
1a>b?a:b较大者说明:手写。
1Math.abs(-3) // 不依赖某个对象3说明:工具性质用静态。
1"hi".length() // 依赖具体串2说明:依赖对象状态用实例。
1Math.round(3.6) // 4最近整数说明:最常用。
1floor(3.6)=3, ceil(3.6)=4向下/向上说明:画图、分页时用。
1static int sq(int x){ return x*x; }类内直接 sq(3)说明:不依赖对象状态时标 static。
1void show(){...} 需对象调对象.show()说明:依赖实例状态则非 static。
1Math.round(x); Math.random();易错点辨析 + 速查表 + 笔记卡,查漏补缺。
1Math m = new Math(); // 试图 new Math1直接用 Math.sqrt(9); // 类名.方法()1int r = Math.sqrt(9); // 类型不匹配1double r = Math.sqrt(9); 或 int r = (int)Math.sqrt(9);1int n = Math.random()*6; // 漏强转1int n = (int)(Math.random() * 6) + 1;| 错误 | 现象 | 修正 |
|---|---|---|
| new Math | 编译错 | 直接类名调用 |
| double→int 漏转 | 类型错 | 加 (int) |
| random 漏转 | 类型错 | 加 (int) |
1忘记强转直接把 random 赋给 int1(int)(Math.random()*6)1想取 1~6 却写 (int)(Math.random()*5)+11*6 +1 才是 1~61直接把 Math.random() 赋给 int 不强转1(int)(Math.random()*6)1Math m = new Math(); m.abs(-3);1int x = Math.abs(-3);1int r = Math.random() * 6; // 想得 0~51int r = (int)(Math.random() * 6);A 基础 / B 提高 / C 挑战 分层练习,点击展开答案与解析。
用 static 修饰、属于类的方法;通过 类名.方法() 直接调用,无需 new。
返回 [0,1) 的双精度随机小数,含 0 不含 1。
round 四舍五入、floor 向下取整、ceil 向上取整。
Math.sqrt(25)=5.0;Math.abs(-9)=9。
(int)(Math.random()*6)+1。
Math.max(a,b)/Math.min(a,b)。
它们是 static,属于类本身。
(int)(Math.random()*11)+10。
随机[0,1)*6→[0,6)小数,强转→0~5,+1→1~6。
2 的 10 次幂,返回 double。
结果同;Math 更直观不易错。
static long fact(int n){long r=1;for(...)r*=i;return r;}
StrictMath 跨平台结果一致,Math 可能用平台优化。
(char)('A'+(int)(Math.random()*26))。
强转去小数得整数索引,偏移调起点,避免范围错位。