0Pricing
Java Academy · 课时

Math 类基础

使用 Math.abs、Math.pow、Math.sqrt、Math.random 以及其他常用的 Math 方法

Math 类基础 是 CoddyKit 上的免费 Java Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Java Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Java Academy 课程共包含 4 节课。

Math 类

java.lang.Math 类提供用于常见数学运算的静态工具方法。它始终可用,无需导入。

绝对值与符号

Math.abs() 返回绝对值(非负数)。Math.signum() 根据符号返回 -1.0、0.0 或 1.0。

System.out.println(Math.abs(-42));      // 42
System.out.println(Math.abs(3.14));     // 3.14
System.out.println(Math.signum(-5.0));  // -1.0
System.out.println(Math.signum(0.0));   //  0.0
System.out.println(Math.signum(7.0));   //  1.0

幂、平方根与立方根

使用 Math.pow(base, exp) 计算幂,使用 Math.sqrt() 计算平方根,使用 Math.cbrt() 计算立方根。

System.out.println(Math.pow(2, 10));  // 1024.0
System.out.println(Math.sqrt(144));   // 12.0
System.out.println(Math.cbrt(27));    // 3.0

// Hypotenuse of a right triangle (a=3, b=4)
double hyp = Math.sqrt(Math.pow(3, 2) + Math.pow(4, 2));
System.out.println(hyp); // 5.0

最小值、最大值与限制范围

Math.min() 和 Math.max() 分别返回两个值中较小或较大的值。将它们组合起来可以实现限制范围的功能,使值保持在指定范围内。

int a = 10, b = 20;
System.out.println(Math.min(a, b)); // 10
System.out.println(Math.max(a, b)); // 20

// Clamp value between 0 and 100
int clamp(int value, int min, int max) {
    return Math.max(min, Math.min(value, max));
}
System.out.println(clamp(150, 0, 100)); // 100
System.out.println(clamp(-5,  0, 100)); // 0
System.out.println(clamp(75,  0, 100)); // 75

向下取整、向上取整与四舍五入

Math.floor() 向下取整,Math.ceil() 向上取整,Math.round() 四舍五入到最接近的整数(中间值向上取整)。

double price = 9.3;
System.out.println(Math.floor(price));  // 9.0
System.out.println(Math.ceil(price));   // 10.0
System.out.println(Math.round(price));  // 9

double shipping = 3.5;
System.out.println(Math.round(shipping)); // 4 (half-up)

// Round to 2 decimal places
double tax = 7.5678;
double rounded = Math.round(tax * 100.0) / 100.0;
System.out.println(rounded); // 7.57

对数与指数

Math.log() 计算自然对数(以 e 为底),Math.log10() 计算以 10 为底的对数,Math.exp() 计算 e^x。

System.out.println(Math.log(Math.E));   // 1.0
System.out.println(Math.log10(1000));   // 3.0
System.out.println(Math.exp(1));        // 2.718281828...

// Change of base: log2(8)
double log2 = Math.log(8) / Math.log(2);
System.out.println(log2); // 3.0

// Compound interest: P * e^(r*t)
double principal = 1000, rate = 0.05, years = 10;
double amount = principal * Math.exp(rate * years);
System.out.printf("Amount: %.2f%n", amount); // 1648.72

三角函数方法

Math 提供以弧度为角度单位的三角函数。请使用 Math.toRadians() 将角度转换为弧度。

double angle = Math.toRadians(45);
System.out.printf("sin(45): %.4f%n", Math.sin(angle)); // 0.7071
System.out.printf("cos(45): %.4f%n", Math.cos(angle)); // 0.7071
System.out.printf("tan(45): %.4f%n", Math.tan(angle)); // 1.0000

// Distance between two GPS coordinates (simplified)
double lat1 = Math.toRadians(40.7128);
double lat2 = Math.toRadians(34.0522);
double deltaLat = lat2 - lat1;
System.out.printf("Delta lat (rad): %.4f%n", deltaLat);

随机数

Math.random() 返回范围为 [0.0, 1.0) 的 double。通过乘法和类型转换,可以得到指定范围内的整数。若需要更好的随机性,请优先使用 java.util.Random。

// Random double [0.0, 1.0)
double rand = Math.random();

// Random int [min, max] inclusive
int min = 1, max = 6;
int dice = (int) (Math.random() * (max - min + 1)) + min;
System.out.println("Dice roll: " + dice); // 1–6

// Prefer java.util.Random for more features
import java.util.Random;
Random rng = new Random();
int secureRoll = rng.nextInt(6) + 1; // cleaner API
System.out.println("Secure roll: " + secureRoll);

常量:PI 与 E

Math.PI 和 Math.E 是表示 π 和欧拉数 e 的 double 常量。请在几何和财务计算中使用它们。

System.out.println(Math.PI); // 3.141592653589793
System.out.println(Math.E);  // 2.718281828459045

// Circle area and circumference
double radius = 7.0;
double area = Math.PI * Math.pow(radius, 2);
double circumference = 2 * Math.PI * radius;

System.out.printf("Area: %.2f%n", area);           // 153.94
System.out.printf("Circumference: %.2f%n", circumference); // 43.98

Math.floorDiv 与 Math.floorMod

Math.floorDiv() 和 Math.floorMod() 能够一致地处理负数;而 / 和 % 运算符处理负数时可能产生令人意外的结果。

// Regular % can be negative with negative dividend
System.out.println(-7 % 3);           // -1 (may surprise you)
System.out.println(Math.floorMod(-7, 3)); // 2  (always non-negative)

// Floor division rounds toward negative infinity
System.out.println(-7 / 3);              // -2 (truncates toward zero)
System.out.println(Math.floorDiv(-7, 3)); // -3 (floors down)

// Use floorMod for cyclic calculations like days of week
int day = Math.floorMod(-1, 7); // Sunday (6), not -1
System.out.println(day); // 6

实践:距离与物理

将 Math 方法应用于物理问题:使用三角函数和平方根计算抛体的射程。

// Projectile range: R = v^2 * sin(2*angle) / g
double velocity = 50.0;   // m/s
double angleDeg = 45.0;   // degrees
double g = 9.81;          // m/s^2

double angleRad = Math.toRadians(angleDeg);
double range = Math.pow(velocity, 2) * Math.sin(2 * angleRad) / g;

System.out.printf("Projectile range: %.1f m%n", range); // 254.8 m

快速检查

Math.floorMod(-7, 3) 的输出是什么?

总结:Math 类

要点:

  • 使用 Math.abs、Math.min、Math.max 进行基本比较
  • 使用 Math.pow、Math.sqrt、Math.cbrt 计算幂和根
  • 使用 Math.floor、Math.ceil、Math.round 进行舍入
  • 使用 Math.log、Math.log10、Math.exp 计算对数和指数
  • Math.sin、Math.cos、Math.tan 使用弧度;请使用 Math.toRadians 进行转换
  • Math.floorMod 能够在循环算术中整洁地处理负数
  • Math.PI 和 Math.E 是内置常量

常见问题解答

「Math 类基础」课时是免费的吗?

是的 — 「Math 类基础」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Java Academy 课程的其余内容,请升级到 CoddyKit PRO。 Java Academy 课程共包含 4 节课。

「Math 类基础」这节课中我会学到什么?

使用 Math.abs、Math.pow、Math.sqrt、Math.random 以及其他常用的 Math 方法 你通过在浏览器中直接运行的动手代码来练习 Java Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Java Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Java Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「Math 类基础」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Java Academy 课中编写并运行代码吗?

能。每节 Java Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Math 类基础
  2. 整数运算与溢出
  3. 使用 BigDecimal 进行财务计算
  4. NumberFormat 与 printf
← 返回 Java Academy