Math(数学)工具类

java.lang.Math类是数学相关的工具类,里面提供了大量的静态方法,完成与数学运算相关的操作。(提供了一系列静态方法用于科学计算。其方法的参数和返回类型一般为double型。)(Math类没有提供公开的构造器。)(私有化构造方法,所有的方法都是静态的。)

Math.abs ( 传入的值 )取绝对值     absolute  完全的,绝对的
Math.ceil ( 传入的值 )向上取整
Math.floor ( 传入的值 )向下取整
Math.round (传入的值 )四舍五入
Math.PI代表近似的圆周率   常量(double)
Math.random ( )

a pseudorandom double greater than or equal to 0.0 and less than 1.0.

大于或等于0.0且小于1.0的伪随机双精度。[0.0,1.0) 左闭右开

如何获取一个随机数:10 - 99 ?

Math.random() * 90 + 10

公式:[ a,b ] :  (int) ( Math.random() * (b - a + 1 ) + a )

Math.max ()返回两个 int 值中的较大值
Math.min ()返回两个 int 值中的较小值
Math.pow (double a,double b)返回 a 的 b 次幂的值            power    次幂, 能力,电
Math.sqrt (传入的值 a)返回 a 的平方根                    square root    平方根          \sqrt{a}        

Math.cbrt (传入的值 a)

返回 a 的立方根                    cube     root    立方根         \sqrt[3]{a}

 计算在  -10.8 到 5.9 之间,绝对值大于6 或者 小于2.1 的整数有多少个?

public class Demo04MathPractise {

    public static void main(String[] args) {

        int count = 0;
        double min = -10.8;
        double max = 5.9;

        for (int i = (int) min; i < max; i++) {
            int abs = Math.abs(i);
            if (abs > 6 || abs < 2.1) {
                System.out.print(i + "  ");
                count++;
            }
        }
        System.out.println("总共有:" + count + "个");
    }
}
D:\Java\jdk-17\bin\java.exe 
-10  -9  -8  -7  -2  -1  0  1  2  总共有:9个

Process finished with exit code 0
public class Demo03Math {

    public static void main(String[] args) {
        System.out.println(Math.abs (-3.14));//3.14

        System.out.println(Math.ceil(3.4));//4.0

        System.out.println(Math.floor(3.4));//3.0

        System.out.println(Math.round(3.5));//4   数学中的四舍五入

        System.out.println(Math.PI); // 3.141592653589793
        
        System.out.println(Math.random());

        System.out.println(Math.max(89, 12));
        System.out.println(Math.min(89, 21));

        System.out.println(Math.pow(2.0, 3.0));

        System.out.println(Math.sqrt(4));
        System.out.println(Math.cbrt(8));
    }
}
D:\Java\jdk-17\bin\java.exe
3.14
4.0
3.0
4
3.141592653589793
0.7034166271154065
89
21
8.0
2.0
2.0

Process finished with exit code 0

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值