File size: 3,412 Bytes
0284885
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package Math_package;

public class Math_demo1 {
    public static void main(String[] args) {
        /*
        public static int       abs(int a)              获取参数绝对值
        public static double    ceil(double a)          向上取整
        public static double    floor(double a)         向下取整
        public static int       round(float a)          四舍五入
        public static int       max(int a, int b)       获取两个int值中的较大值
        public static double    pow(double a, double b) 返回a的b次幂的值
        public static double    sqrt(double a)          返回a的平方根
        public static double    cbrt(double a)          返回a的立方根
        public static double    random()                返回值为double的随机值,范围[0.0, 1.0)
         */

        //abs   获取参数绝对值
        System.out.println(Math.abs(-88));
        System.out.println(Math.abs(88));
        //bug:
        //以int类型为例,取值范围: -2147483648 ~ 2147483647
        //如果没有正数与负数对应,那么传递负数结果有误
        //-2147483648 没有正数与之对应,所以abs结果产生bug
        System.out.println(Math.abs(-2147483648));
        //System.out.println(Math.absExact(-2147483648));
        System.out.println("-----------------------");

        //进一法:往数轴的正方向进一
        System.out.println(Math.ceil(12.34));
        System.out.println(Math.ceil(12.54));
        System.out.println(Math.ceil(-12.34));
        System.out.println(Math.ceil(-12.54));
        System.out.println("-----------------------");

        //去尾法:往数轴的负方向进一
        System.out.println(Math.floor(12.34));
        System.out.println(Math.floor(12.54));
        System.out.println(Math.floor(-12.34));
        System.out.println(Math.floor(-12.54));
        System.out.println("-----------------------");

        //public static int       round(float a)          四舍五入
        System.out.println(Math.round(12.34));
        System.out.println(Math.round(12.54));
        System.out.println(Math.round(-12.34));
        System.out.println(Math.round(-12.54));
        System.out.println("-----------------------");

        //public static int       max(int a, int b)       获取两个int值中的较大值
        System.out.println(Math.max(20, 30));
        System.out.println("-----------------------");

        //获取两个整数的较小值
        System.out.println(Math.min(20, 30));
        System.out.println("-----------------------");

        //获取a的b次幂
        System.out.println(Math.pow(2, 3));
        //细节:
        //如果第二个参数 0~1之间的小数
        System.out.println(Math.pow(4, 0.5));
        System.out.println(Math.pow(2, -2));
        //建议:
        //第二个参数:一般传递大于等于1的正整数
        System.out.println("-----------------------");

        System.out.println(Math.sqrt(4));
        System.out.println(Math.cbrt(8));
        System.out.println("-----------------------");

        for (int i = 0; i < 10; i++) {
            System.out.println(Math.floor(Math.random() * 100) + 1);
            //Math.random() [0.0 , 1.0)
            //* 100     [0.0 , 100.0)
            //floor     去掉了后面的小数
            //+1        [1.0 , 100.0]
        }


    }
}