数字与字符串系列教材 (三)- Java Math类常用方法

java.lang.Math提供了一些常用的数学运算方法,并且都是以静态方法的形式存在

步骤1:四舍五入, 随机数,开方,次方,π,自然常数
步骤2:练习-数学方法
步骤3:答案-数学方法
步骤4:练习-质数
步骤5:答案-质数

步骤 1 : 四舍五入, 随机数,开方,次方,π,自然常数

package digit;

  

public class TestNumber {

  

    public static void main(String[] args) {

        float f1 = 5.4f;

        float f2 = 5.5f;

        //5.4四舍五入即5

        System.out.println(Math.round(f1));

        //5.5四舍五入即6

        System.out.println(Math.round(f2));

         

        //得到一个0-1之间的随机浮点数(取不到1)

        System.out.println(Math.random());

         

        //得到一个0-10之间的随机整数 (取不到10)

        System.out.println((int)( Math.random()*10));

        //开方

        System.out.println(Math.sqrt(9));

        //次方(2的4次方)

        System.out.println(Math.pow(2,4));

         

        //π

        System.out.println(Math.PI);

         

        //自然常数

        System.out.println(Math.E);

    }

}


更多内容,点击了解: https://how2j.cn/k/number-string/number-string-math/319.html

原文地址:https://www.cnblogs.com/Lanht/p/12615529.html