AP CSA 1.11:使用 Math 类
In the previous lesson, we learned how to call static class methods. In this lesson, we focus on one important built-in class: Math.
上一课我们学习了如何调用 static 类方法。这一课,我们重点学习一个 AP CSA 常用的内置类:Math。
The
Mathclass provides static methods for common calculations and random values.Math类提供用于常见计算和随机数的 static 方法。
Core Concepts:
核心知识点
1. Main Idea
核心理解
The Math class contains static methods for common mathematical operations.
Math 类包含一些常见数学运算的 static 方法。
Because these methods are static, we call them with the class name:
因为这些方法是 static 方法,所以调用时使用类名:
Math.methodName(arguments);
Examples:
System.out.println(Math.abs(-5));
System.out.println(Math.sqrt(9));
System.out.println(Math.pow(2, 3));
Output:
5
3.0
8.0
For AP CSA, you do not need to memorize every Java Math method. Focus on the methods listed in the AP CSA Java Quick Reference.
对于 AP CSA,你不需要记住 Java 里所有的 Math 方法。重点掌握 AP CSA Java Quick Reference 上列出的方法。
2. AP CSA Math Methods
AP CSA 中的 Math 方法
| Method | Meaning | Return Type | Example |
|---|---|---|---|
Math.abs(int x) |
absolute value | int |
Math.abs(-4) → 4 |
Math.abs(double x) |
absolute value | double |
Math.abs(-4.5) → 4.5 |
Math.sqrt(double x) |
positive square root | double |
Math.sqrt(9) → 3.0 |
Math.pow(double base, double exp) |
power | double |
Math.pow(2, 3) → 8.0 |
Math.random() |
random double | double |
0.0 <= value < 1.0 |
Two return-type details matter a lot:
两个返回类型细节很重要:
double a = Math.sqrt(9);
double b = Math.pow(2, 3);
Math.sqrt and Math.pow return double, even if the result looks like a whole number.
Math.sqrt 和 Math.pow 返回 double,即使结果看起来是整数。
3. Math.abs and Distance
Math.abs 与距离
Math.abs(x) returns the absolute value of x.
Math.abs(x) 返回 x 的绝对值。
System.out.println(Math.abs(6));
System.out.println(Math.abs(-6));
System.out.println(Math.abs(-3.5));
Output:
6
6
3.5
On a number line, the distance between two numbers a and b is:
在数轴上,两个数 a 和 b 之间的距离是:
Math.abs(a - b)
Example:
double a = -3;
double b = 1;
System.out.println(Math.abs(a - b));
Output:
4.0
Do not use this:
Math.abs(a) - Math.abs(b)
That does not correctly compute the distance between a and b.
这不能正确计算 a 和 b 之间的距离。
4. Math.sqrt and Math.pow
平方根与幂运算
Math.sqrt(x) takes one argument and returns the positive square root.
Math.sqrt(x) 接收一个实参,返回正平方根。
System.out.println(Math.sqrt(9));
System.out.println(Math.sqrt(16));
Output:
3.0
4.0
Math.pow(base, exponent) takes two arguments and returns:
Math.pow(base, exponent) 接收两个实参,返回:
base raised to the exponent
也就是:
base 的 exponent 次方
Example:
System.out.println(Math.pow(2, 3));
System.out.println(Math.pow(10, 2));
Output:
8.0
100.0
Both sqrt and pow return double.
sqrt 和 pow 都返回 double。
5. Syntax: Argument Count Matters
语法:实参数量要匹配
AP questions often test whether a method call is syntactically correct.
AP 题经常考一个方法调用在语法上是否正确。
Math.sqrt needs exactly one argument.
Math.sqrt 必须有且只有一个实参。
Correct:
Math.sqrt(2)
Math.sqrt(2 + 3)
Math.sqrt(Math.sqrt(16))
Wrong:
Math.sqrt()
Math.sqrt(2, 4)
Math.sqrt 2
Math.pow needs exactly two arguments.
Math.pow 必须有两个实参。
Correct:
Math.pow(3, 2)
Wrong:
Math.pow(3)
6. Math.random() Range
Math.random() 的范围
Math.random() returns a double in this range:
Math.random() 返回一个 double,范围是:
0.0 <= value < 1.0
That means:
这意味着:
-
it can return
0.0 -
it never returns
1.0 -
可能返回
0.0 -
不会返回
1.0
Example:
System.out.println(Math.random());
System.out.println(Math.random());
Possible output:
0.374921
0.812305
The exact numbers change each time the program runs.
每次运行,具体数字通常都会变化。
7. Random Integers
随机整数
To generate random integers, we usually multiply, cast to int, and then shift the range.
生成随机整数时,通常先乘以范围大小,再转换成 int,最后平移范围。
From 0 to 9
从 0 到 9
int n = (int)(Math.random() * 10);
Possible values:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
From 1 to 10
从 1 到 10
int n = (int)(Math.random() * 10) + 1;
Possible values:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
General formula
通用公式
To generate an integer from min to max, inclusive:
要生成从 min 到 max,包含两端的整数:
int n = (int)(Math.random() * (max - min + 1)) + min;
The range size is:
范围大小是:
max - min + 1
Example: random integer from 25 to 60, inclusive:
例如:生成 25 到 60,包含两端的随机整数:
int rn = (int)(Math.random() * 36) + 25;
Why 36?
为什么是 36?
60 - 25 + 1 = 36
8. Parentheses Matter When Casting
类型转换时括号很重要
This is correct:
正确写法:
int n = (int)(Math.random() * 10);
This is wrong:
错误写法:
int n = (int)Math.random() * 10;
Why?
为什么?
(int)Math.random() becomes 0, because Math.random() is always less than 1.0.
(int)Math.random() 会变成 0,因为 Math.random() 永远小于 1.0。
So:
(int)Math.random() * 10
= 0 * 10
= 0
If you write:
int n = (int)Math.random() * 10 + 1;
the result is always:
1
because (int)Math.random() is always 0.
因为 (int)Math.random() 总是 0。
9. Probability with Math.random()
用 Math.random() 判断概率
Since Math.random() is between 0.0 and 1.0, comparisons can represent probabilities.
因为 Math.random() 在 0.0 到 1.0 之间,所以比较表达式可以表示概率。
Math.random() < 0.4
This is true about 40% of the time.
这个表达式大约 40% 的时候为 true。
Math.random() > 0.25
This is true about 75% of the time.
这个表达式大约 75% 的时候为 true。
Do not use equality for probability questions:
概率题里不要用等号判断:
Math.random() == 0.4
This is not a good way to represent 40%.
这不是表示 40% 概率的正确方式。
10. Common Beginner Mistakes
常见初学者错误
| Mistake | Wrong Code | Why Wrong | Correct Code | 中文解释 |
|---|---|---|---|---|
Forgetting Math. |
sqrt(9) |
sqrt belongs to Math |
Math.sqrt(9) |
调用 Math 方法要写 Math. |
| Wrong number of arguments | Math.sqrt(2, 4) |
sqrt takes one argument |
Math.sqrt(2 + 4) |
sqrt 只能接收一个实参 |
Assigning double to int |
int x = Math.sqrt(9); |
sqrt returns double |
double x = Math.sqrt(9); |
返回值类型不匹配 |
| Casting too early | (int)Math.random() * 10 |
Always becomes 0 |
(int)(Math.random() * 10) |
先乘,再转 int |
| Off-by-one range | (int)(Math.random() * 5) for 1–5 |
gives 0–4 | (int)(Math.random() * 5) + 1 |
随机范围少平移一步 |
| Wrong distance formula | Math.abs(a) - Math.abs(b) |
not distance between two numbers | Math.abs(a - b) |
两点距离是差的绝对值 |
11. Debugging Example
调试例子
Buggy code:
int n = (int)Math.random() * 10 + 1;
System.out.println(n);
Problem:
This code always prints 1.
问题:
这段代码总是打印 1。
Why?
为什么?
(int)Math.random() = 0
0 * 10 + 1 = 1
Fixed code:
int n = (int)(Math.random() * 10) + 1;
System.out.println(n);
Now n can be any integer from 1 to 10.
现在 n 可以是 1 到 10 之间的任意整数。
| Bug | Type | Fix |
|---|---|---|
(int)Math.random() * 10 + 1 |
logic error | (int)(Math.random() * 10) + 1 |
int x = Math.pow(2, 3); |
compile-time error | double x = Math.pow(2, 3); |
Math.sqrt() |
compile-time error | pass exactly one argument |
12. Mini Practice
小练习
Practice 1: Choose the Output Type
练习 1:判断输出类型
What is printed?
System.out.println(Math.sqrt(9));
System.out.println(Math.pow(2, 3));
Answer:
3.0
8.0
Explanation: both methods return double.
解释:这两个方法都返回 double。
Practice 2: Choose the Correct Expression
练习 2:选择正确表达式
Which expression computes the distance between a and b?
A. Math.abs(a - b)
B. Math.abs(a) - Math.abs(b)
C. Math.abs(a + b)
Answer:
A. Math.abs(a - b)
Explanation: distance on a number line is the absolute value of the difference.
解释:数轴上两点之间的距离是“差的绝对值”。
Practice 3: Random Range
练习 3:随机数范围
Which expression gives a random integer from 1 to 5, inclusive?
A. (int)(Math.random() * 5)
B. (int)(Math.random() * 6)
C. (int)(Math.random() * 5) + 1
Answer:
C. (int)(Math.random() * 5) + 1
Explanation: (int)(Math.random() * 5) gives 0 to 4; adding 1 shifts it to 1 to 5.
解释:(int)(Math.random() * 5) 得到 0 到 4;加 1 后变成 1 到 5。
Practice 4: Fix the Code
练习 4:修复代码
The goal is to generate a random integer from 25 to 60, inclusive.
Buggy code:
int rn = (int)(Math.random() * 60) + 25;
Fixed code:
int rn = (int)(Math.random() * 36) + 25;
Explanation:
60 - 25 + 1 = 36
解释:范围大小是 36,不是 60。
Quick Checklist
快速检查清单
Before answering a Math class question, check:
做 Math 类题目前,检查:
- Is the method called with
Math.? - Does the method use the correct number of arguments?
- Does
Math.sqrthave exactly one argument? - Does
Math.powhave exactly two arguments? - What type does the method return?
- Are you storing a
doublereturn value in adoublevariable? - Is
Math.random()treated as0.0 <= value < 1.0? - For random integers, did you multiply before casting to
int? - Are the casting parentheses correct?
- For an inclusive range, did you use
max - min + 1? - Are you accidentally off by one?
- For distance, did you use
Math.abs(a - b)?