Java基础 Math常用方法使用

    public void math01(){
        //1.abs 绝对值
        System.out.println("abs绝对值");
        int abs = Math.abs(-7);
        System.out.println(abs);
        //2.求幂
        System.out.println("pow求幂");
        double pow = Math.pow(2, 4);
        System.out.println(pow);
        //3.ceil向上取整  >=该参数的最小整数
        System.out.println("ceil向上取整");
        double ceil = Math.ceil(3.2);
        System.out.println(ceil);
        //4.flooe向下取整<=该参数的最小整数
        System.out.println("flooe向下取整");
        double floor = Math.floor(4.8);
        System.out.println(floor);
        //5.round 四舍五入
        System.out.println("round 四舍五入");
        long round = Math.round(5.5);
        System.out.println(round);
        //6.sqrt 求开方
        System.out.println("sqrt 求开方");
        double sqrt = Math.sqrt(9.0);
        System.out.println(sqrt);
        System.out.println("1-10随机数");
        //7.random [0,1) Math.random()*(b-a) 返回时0<=数<=b-a
        for (int i = 0; i <10 ; i++) {
            System.out.println((int)(Math.random()*(10)+1));

        }
        System.out.println("最大值");
        //9.max min 最大值和最小值
        int max = Math.max(1, 8);
        System.out.println(max);
        System.out.println("最小值");
        int min = Math.min(8, 2);
        System.out.println(min);


    }

运行截图:

Java基础 Math常用方法使用_第1张图片

你可能感兴趣的:(java常用类,java,jvm,开发语言)