Math和Random类常用方法

一、Math类

1、常用方法

1、abs 绝对值

2、acos,asin,atan,cos,sin,tan 三角函数

3、 sqrt 平方根

4、pow(double a, double b) a的b次幂

5、max(double a, double b) 取大值

6、min(double a, double b) 取小值

7、ceil(double a) 大于a的最小整数

8、floor(double a) 小于a的最大整数

9、random() 返回 0.0 到 1.0 的随机数

10、long round(double a) double型的数据a转换为long型(四舍五入)

11、toDegrees(double angrad) 弧度->角度

12、toRadians(double angdeg) 角度->弧度

2、查用的方法代码

//取整相关操作
System.out.println(Math.ceil(3.2));     //4.0
System.out.println(Math.floor(3.2));    //3.0
System.out.println(Math.round(3.2));    //3
System.out.println(Math.round(3.8));    //4
//绝对值、开方、a的b次幂等操作
System.out.println(Math.abs(-45));      // 绝对值(45)
System.out.println(Math.sqrt(64));      //平方根(8.0)
System.out.println(Math.pow(5, 2));     //a的b次幂(25.0)
System.out.println(Math.pow(2, 5));     //32.0
//Math类中常用的常量
System.out.println(Math.PI);            //3.141592653589793
System.out.println(Math.E);             //2.718281828459045
//随机数
System.out.println(Math.random());      // [0,1)0.842255731246934

二、Random类

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));
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ha_lydms

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值