API-Math

Java 的 Math 类提供了许多用于数学运算的方法。以下是一些常用的方法:

常用方法

  1. 基本运算

    • Math.abs(int a):返回绝对值。
    • Math.max(int a, int b):返回较大值。
    • Math.min(int a, int b):返回较小值。
  2. 指数运算

    • Math.pow(double a, double b):返回 ( a^b )。
    • Math.sqrt(double a):返回平方根。
  3. 对数运算

    • Math.log(double a):返回自然对数(以 ( e ) 为底)。
    • Math.log10(double a):返回以 10 为底的对数。
  4. 三角函数

    • Math.sin(double angle):返回角度的正弦值。
    • Math.cos(double angle):返回角度的余弦值。
    • Math.tan(double angle):返回角度的正切值。
  5. 反三角函数

    • Math.asin(double a):返回反正弦(弧度)。
    • Math.acos(double a):返回反余弦(弧度)。
    • Math.atan(double a):返回反正切(弧度)。
  6. 取整

    • Math.ceil(double a):向上取整。
    • Math.floor(double a):向下取整。
    • Math.round(double a):四舍五入。
  7. 随机数

    • Math.random():返回 [0.0, 1.0) 之间的随机数。

示例代码

public class MathExample {
    public static void main(String[] args) {
        System.out.println(Math.abs(-5));           // 5
        System.out.println(Math.max(10, 20));       // 20
        System.out.println(Math.sqrt(16));          // 4.0
        System.out.println(Math.pow(2, 3));         // 8.0
        System.out.println(Math.sin(Math.PI / 2));  // 1.0 
        System.out.println(Math.random());          // Random number
    }
}

Math 类的方法都是静态的,因此可以直接通过类名调用,无需创建对象。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值