常用类四(Math类)

       

目录

Math 类的常用方法:

Random 类的常用方法:

         java.lang.Math 提供了一系列静态方法用于科学计算;其方法的参数和返回值类型一般 为 double 型。如果需要更加强大的数学运算能力,计算高等数学中的相关内容,可以使用 apache commons 下面的 Math 类库。

Math 类的常用方法:

abs 绝对值
acos,asin,atan,cos,sin,tan 三角函数
sqrt 平方根
pow(double a, double b) a 的 b 次幂
max(double a, double b) 取大值
min(double a, double b) 取小值
ceil(double a) 大于 a 的最小整数
floor(double a) 小于 a 的最大整数
random() 返回 0.0 到 1.0 的随机数
long round(double a) double 型的数据 a 转换为 long 型(四舍五入)
toDegrees(double angrad) 弧度->角度
toRadians(double angdeg) 角度->弧度

public class TestMath {
    public static void main(String[ ] args) {
        //取整相关操作
        System.out.println(Math.ceil(3.2));
        System.out.println(Math.floor(3.2));
        System.out.println(Math.round(3.2));
        System.out.println(Math.round(3.8));
        //绝对值、开方、a 的 b 次幂等操作
        System.out.println(Math.abs(-45));
        System.out.println(Math.sqrt(64));
        System.out.println(Math.pow(5, 2));
        System.out.println(Math.pow(2, 5));
        //Math 类中常用的常量
        System.out.println(Math.PI);
        System.out.println(Math.E);
        //随机数
        System.out.println(Math.random());// [0,1)
    }
}

        Math 类中虽然为我们提供了产生随机数的方法 Math.random(),但是通常我们需要的随 机数范围并不是[0, 1)之间的 double 类型的数据,这就需要对其进行一些复杂的运算。如果 使用 Math.random()计算过于复杂的话,我们可以使用例外一种方式得到随机数,即 Random 类,这个类是专门用来生成随机数的,并且 Math.random()底层调用的就是 Random 的 nextDouble()方法。

Random 类的常用方法:

import java.util.Random;

public class TestRandom {
    public static void main(String[ ] args) {
        Random rand = new Random();
        //随机生成[0,1)之间的 double 类型的数据
        System.out.println(rand.nextDouble());
        //随机生成 int 类型允许范围之内的整型数据
        System.out.println(rand.nextInt());
        //随机生成[0,1)之间的 float 类型的数据
        System.out.println(rand.nextFloat());
        //随机生成 false 或者 true
        System.out.println(rand.nextBoolean());
        //随机生成[0,10)之间的 int 类型的数据
        System.out.print(rand.nextInt(10));
        //随机生成[20,30)之间的 int 类型的数据
        System.out.print(20 + rand.nextInt(10));
        //随机生成[20,30)之间的 int 类型的数据(此种方法计算较为复杂)
        System.out.print(20 + (int) (rand.nextDouble() * 10));
    }
}

Random 类位于 java.util 包下。

相关推荐

  1. Math

    2024-03-29 05:34:03       32 阅读
  2. ——包装

    2024-03-29 05:34:03       36 阅读
  3. C#

    2024-03-29 05:34:03       45 阅读
  4. C++

    2024-03-29 05:34:03       29 阅读
  5. html

    2024-03-29 05:34:03       60 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-29 05:34:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-29 05:34:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-29 05:34:03       82 阅读
  4. Python语言-面向对象

    2024-03-29 05:34:03       91 阅读

热门阅读

  1. Django(四)-搭建第一个应用(3)

    2024-03-29 05:34:03       46 阅读
  2. c++中2种返回变量类型名称的方法

    2024-03-29 05:34:03       38 阅读
  3. 如何系统的学习 C#

    2024-03-29 05:34:03       42 阅读
  4. 利用图像识别进行疾病诊断

    2024-03-29 05:34:03       38 阅读
  5. 组件上使用 v-for

    2024-03-29 05:34:03       39 阅读
  6. 前端a4纸尺寸转像素尺寸

    2024-03-29 05:34:03       43 阅读
  7. 浪潮 M5系列服务器IPMI无法监控存储RAID卡问题.

    2024-03-29 05:34:03       106 阅读
  8. Golang基础-5

    2024-03-29 05:34:03       40 阅读
  9. C++之struct和class区别

    2024-03-29 05:34:03       41 阅读