java.lang.math_java.lang.Math中的基本方法

java.lang.Math类提供的方法都是static的,“静态引入

”使得不必每次在调用类方法时都在方法前写上类名:

import static java.lang.Math.*;

这样在调用Math的方法时就能够简单地写出方法名,比如:

cos(radians);

----------------------------------------------------------

1、基本方法:

abs, max, min, ceil, floor, rint, round

重载abs方法,返回一个数(int、long、float、double)的绝对值

重载max和min方法,返回两个数(int、long、float、double)的最大值和最小值

public static double ceil(double x);  //向上取整,返回double

public static double floor(double x);  //向下取整,返回double

public static double rint(double x);  //以double值返回与x最接近的整数,假设x到两个整数的距离相等,返回当中的偶数

public static long round(double x);  //返回(long)Math.floor(x+0.5);

public static int round(float x);  //返回(int)Math.floor(x+0.5);

2、指数和对数方法:

(Math.E = 2.7183)

exp, log, pow, sqrt

3、三角函数:

sin, cos, tan, asin, acos, atan

double atan2

(double y, double x);  //将直角坐标系的坐标(x, y)转变为极坐标中的坐标(r, theta),并返回角度thera

public static double toDegrees(double radians);

public static double toRadians(double degree);

4、随机数

0.0 <= Math.random() < 1.0

假设要得到一个[0, 10)之间的随机整数:

int number = (int)(Math.random() * 10);

假设要得到一个[50, 100)之间的随机整数:

int number = 50 + (int)(Math.random() * 50);

假设要得到一个(a, a+b]之间的随机整数:

int number = a + (int)(Math.random() * b);

使用Math.Random()能够得到单个随机数,但若要得到一系列随机数,则能够使用java.util.Random

类,通过创建对象,调用对应的方法实现。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值