Math类
1、基本介绍
Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
2、常见方法(一般都是静态)
//看看Math常用的方法(静态方法)
//1.abs 绝对值
int abs = Math.abs(-9);
System.out.println(abs);//9
//2.pow 求幂
double pow = Math.pow(2, 4);//2的4次方
System.out.println(pow);//16
//3.ceil (天花板的意思)向上取整,返回>=该参数的最小整数(转成double);
double ceil = Math.ceil(3.9);
System.out.println(ceil);//4.0
double ceil2 = Math.ceil(-3.0001);
System.out.println(ceil2);//-3.0
//4.floor 向下取整,返回<=该参数的最大整数(转成double)
double floor = Math.floor(4.001);
System.out.println(floor);//4.0
double floor2 = Math.floor(-4.99999);
System.out.println(floor2);//-5.0
//5.round 四舍五入 Math.floor(该参数+0.5)
long round = Math.round(5.51);
System.out.println(round);//6
long round2 = Math.round(-5.001);
System.out.println(round2);//-5
//6.sqrt 求开方
double sqrt = Math.sqrt(9.0);
System.out.println(sqrt);//3.0
//7.random 求随机数
// random 返回的是 0 <= x < 1 之间的一个随机小数
for(int i = 0; i < 10; i++) {
System.out.println(Math.random());
}
//8. max , min 返回最大值和最小值
int min = Math.min(1, 9);
int max = Math.max(45, 90);
System.out.println("min=" + min);
System.out.println("max=" + max);
思考:请写出获取 a-b之间的一个随机整数,a,b均为整数 ,比如 a = 2, b=7
即返回一个数 x : 2 <= x <= 7
解读
Math.random() * (b-a) 返回的就是 0 <= 数 < b-a
如果要包含b-a的话,就需要 Math.random() * (b-a +1)
(1)(int)(a) <= x <= (int)(a + Math.random() * (b-a +1) )
(2)公式就是 (int)(a + Math.random() * (b-a +1) )
Math.random()∈[0,1)
a + Math.random() * (b-a) ∈[a,b)
a + Math.random() * (b-a +1) ∈[a,b]
for(int i = 0; i < 100; i++) {
System.out.println((int)(2 + Math.random() * (7 - 2 + 1)));
}