Math类
Math类常见的方法
1.abs: 绝对值
2.pow :求幂
3.ceil : 向上取整
4.floor :向下取整
5.rond : 四舍五入
6.sqrt : 求开方
7.random : 求随机数
8.max : 求两个数的最大值
9.min : 求两个数的最小值
代码编写
public cllass MathMethod{
public static void main(String[] args){
//Math 常用方法(静态方法)
//1.abs 绝对值
int abs = Math.abs(-9);
System.out.println(abs);//结果: 9
//2.求幂
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
//4.floor 向下取整,返回<= 该查参数的最大整数(转成double)
double floor = Math.floor(-4.999);
System.out.println(floor);//-5.0
//5.round 四舍五入 Math.floor(该参数+0.5)
long round = Math.round(5.51);
System.out.println(round);//6
//6.sqrt 求开方
double sqrt = Math.sqrt(9);
System.out.println(sqrt);//3.0
// 7.random 求随机数
// random 返回的是 0<= x < 1 之间的一个随机小数
// 请写出获取 a-b 之间的一个随机整数,a,b均为整数?2-7
//解读 Math.random() * (b-a) 返回的就是 0 <= 数 <= b-a
//(1). (int)(a) <= x <= (int)(a + Math.random() * (b - a + 1))
//(2) 使用具体的数介绍 a = 2 b = 7
// (int)(a + Math.random() * (b - a + 1)) = (int)(2 + Math.random() * 6)
// Math.random() * 6 返回的是 0 <= x < 6 小数
// 2 + Math.random() * 6 返回的就是 2 <= x <= 7
// (int)(2 + Math.random() * 6) = 2 <= x <= 7
// (3) 公式: (int)(a + Math.random() * (b - a + 1))
for (int i = 0; i < 10; i++) {
System.out.println((int)(1 + Math.random() * (7 - 2 +1))+"\t");
//max , min 返回最大值和最小值
int min = Math.min(1,9);
int max = Math.max(45,90);
System.out.println("min"+min);
System.out.println("max"+max);
}
}
}