The Math Class
引入本课内容
| English | 释义 | 拼音 |
|---|---|---|
| integer/ˈɪntɪdʒə/ | 整数 | zhěng shù |
| Scaling | 缩放 | suō fàng |
| truncating | 截断 | jié duàn |
| shifting | 平移 | píng yí |
| class/klæs/ | 类 | lèi |
| library/ˈlaɪbrəri/ | 库 | kù |
| class methods | 类方法 | lèi fāng fǎ |
| constants | 常量 | cháng liàng |
| import/ɪmˈpɔːt/ | 导入 | dǎo rù |
| object/ˈɒbdʒekt/ | 对象 | duì xiàng |
| powers | 幂 | mì |
| roots/ruːts/ | 方根 | fāng gēn |
| rounding/ˈraʊndɪŋ/ | 舍入 | shě rù |
| absolute value/ˈæbsəluːt ˈvæljuː/ | 绝对值 | jué duì zhí |
| input/ˈɪnpʊt/ | 输入 | shū rù |
| output type | 输出类型 | shū chū lèi xíng |
| square root/skweə ruːt/ | 平方根 | píng fāng gēn |
| upper bound/ˈʌpə baʊnd/ | 上界 | shàng jiè |
| double/ˈdʌbl/ | 双精度浮点型 | shuāng jīng dù fú diǎn xíng |
| boundary behavior | 边界行为 | biān jiè xíng wéi |
| NaN | 非数字 | fēi shù zì |
| static methods | 静态方法 | jìng tài fāng fǎ |
追踪随机范围的两端
- 票据需要从 4 到 9(包含两端)的整数标签。使用 (int)(Math.random() * 6) + 4。
- 缩放产生从 0 到但不包括 6 的值;截断产生从 0 到 5 的值;平移产生从 4 到 9 的值。

Math 工具箱
Math类是一个包含现成数学类方法和常量的库。无需导入,也无需创建对象——直接调用Math.something(...)即可。- 它涵盖幂运算、开方、舍入、绝对值、最小值和最大值等。选择输入、输出类型及数学含义与任务匹配的方法;并非所有输入都有常规的有限结果。
AP 常用方法
Math.abs(x)— 绝对值:Math.abs(-5)的绝对值为5。Math.pow(b, e)—b的e次方:Math.pow(2, 3)的结果为8.0。Math.sqrt(x)— 平方根:Math.sqrt(9)的结果为3.0。Math.max(a, b)/Math.min(a, b)— 两个值中的较大值 / 较小值。
随机数
Math.random()返回一个double,范围为 从0.0到(但不包括)1.0。通过缩放和平移可以得到所需范围:(int)(Math.random() * 6)生成0–5。- 添加
1可模拟掷骰子:(int)(Math.random() * 6) + 1给出1–6。对于一个正且适中的整数宽度 n,若生成的标签能用 int 表示,(int)(Math.random() * n) + start会给出 n 个可能的整数标签。对于从 4 到 9(包含两端)的范围,n 为9 - 4 + 1 = 6。
Building a die roll from Math.random()
Scale, truncate, then shift to get 1-6.
What does Math.pow(2, 3) equal (as a number)?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
2 to the power 3 = 8 (returned as the double 8.0).
Math.sqrt(9) returns a value of which type?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
sqrt always returns a double — cast to use it as an int.
Math.random() returns a double in which range?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
[0.0, 1.0) — the upper bound 1.0 is never returned.
Which expression gives a random integer from 1 to 6?
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
Scale by 6 → 0..5, truncate, then +1 → 1..6.
What is Math.abs(-5)?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Absolute value of −5 is 5.
留意返回类型
Math.pow和Math.sqrt总是返回double(例如8.0,而非8)。若要将结果作为int使用,必须进行强制类型转换:(int) Math.pow(2, 3)。Math.random()也返回double—— 应在缩放之后而非之前进行强制类型转换。混淆返回的int和double是使用 Math 类时的典型错误。
Math.pow 和 Math.sqrt 返回的是 double,而非 int。 Math.pow(2, 3) 的结果是 8.0;将其存储在 double 中,或通过强制类型转换将其作为 int 使用。此外,Math.random() 返回一个 double,范围为 [0.0, 1.0) —— 永远不会返回上界 1.0。使用 (int)(Math.random() * n) + start 构建整数范围。
Order the operations that build labels 4 through 9.
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Scaling must happen before the cast; otherwise every initial cast is zero.
For labels 4 through 9, substitute u=0 into (int)(u * 6) + 4. What label results?
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
The scaled and cast value is zero, then 4 is added.
Math.sqrt(-1) throws an exception instead of returning a double result.
难度 1/5 · 今日首次完成可获 100 金币及少量额外奖励
It returns the special double value NaN; this is different from an exception.
一次随机掷骰子(1–6):
Math.random()→ 一个双精度浮点数,如0.42。Math.random() * 6→ 从0.0到接近但不到6.0(如2.52)。(int)(Math.random() * 6) + 1→ 截断为2,再+1→3。
将推理应用于新情况
- 先进行强制类型转换再缩放,
(int)Math.random() * 6 + 4总是得到 4。检查(int)(u * 6) + 4中的 u=0 和略小于 1 的可表示值:可能的端点是 4 和 9;u=1 超出了随机方法的范围。 - 库方法同样具有边界行为:
Math.sqrt(-1)返回 NaN(非数字),Math.abs(Integer.MIN_VALUE)返回原来的负 int 值,因为它的正数绝对值超出了 int 的表示范围。常规示例 abs(-5)=5 依然成立。
Math 类提供静态方法:abs、pow、sqrt、max、min 和 random。pow/sqrt 返回一个 double(要作为 int 使用需进行强制类型转换)。Math.random() 返回一个 double,范围为 [0.0, 1.0);可使用 (int)(Math.random() * n) + start 构建整数范围。
出了什么问题?只有网站团队能看到这些信息。